Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    using the process status (ps) command in a bash script


    I was wondering if someone could help me out. I am new to scripting and been working on this bash script for awhile now. I've been researching this problem, but I can't seem to find a solution. I was wondering if someone could please help me out. Here is my script:


    NO="ps -ef | grep NO | grep -v grep"
    NOWC=`ps -ef | grep NO | grep -v grep | wc -l`

    for CLIENT in $*
    ssh $CLIENT -l root "if [ $NOWC -gt 0 ] ; then
    echo "there are $NOWC processes running"
    echo "Here is the list of process"
    eval $NO
    echo "Cannot find that process"
    echo "Good Bye"


    exit 0

    Here is the problem. I cannot get this script to run the "ps -ef" command on the client. It get its value from the host machine that I am running this script from. I need this command to execute on the client. When I run the command (ps -ef | grep NO | grep -v grep) on the client, I get something back. Here is what I get when I try to debug the script.

    XX# ./ XXXXX
    + NO='ps -ef | grep NO | grep -v grep'
    ++ ps -ef
    ++ grep NO
    ++ grep -v grep
    ++ wc -l
    + NOWC=0
    + for CLIENT in '$*'
    + ssh XXXXX -l root 'if [ 0 -gt 0 ] ; then
    echo there' are processes 'running
    echo Here' is the list of 'process
    eval ps -ef | grep NO | grep -v grep
    echo Cannot' find that 'process
    echo Good' 'Bye

    Cannot find that process
    Good Bye
    + exit 0

    Any input would be appreciated and thank you in advance for your help!!
    Last edited by young_linux; 05-28-2009 at 02:01 AM. Reason: Needed to change the $JOBNOWC variable to $NOWC. Changed this on my original script but forgot to change it on this post.

  2. #2 not entirely sure..but as i remember those variables are not set on the client machine,which is where ssh is looking for them...i may be wrong...
    i seen one which may be related to your problem they sai they had to wrap the ssh command in a variable and execute the variable so maybe:
    sshcmd="ssh $CLIENT -l root "if [ $NOWC -gt 0 ] ; then
    echo "there are $NOWC processes running"
    echo "Here is the list of process"
    eval $NO
    echo "Cannot find that process"
    echo "Good Bye"

    and then just call $sshcmd
    (as this evaluates the variables before running, while the command you gave evaluates the variables after)

  3. #3
    Linux User
    Join Date
    May 2008
    NYC, moved from KS & MO
    The reason why your script didn't work is that NOWC line is executed on the local host before it's sent over to the remote host.

    As the_ultimate_samurai said, you can wrap up a long command line after the ssh command but it's gonna be quite messy. The simplest way I can think of is to write that process query script and store it to the remote host's disk (for example, /root/process_script), make it executable, then
    change the ssh line on your localhost machine to
    ssh $CLIENT -l root /root/process_script

  4. $spacer_open
  5. #4


    Thank you both for yalls posts and suggestions. You were both right and after going back an adjusting my script, I finally (with yalls help) was able to get what I wanted. Here is the script that works:

    for CLIENT in $*
    NO=`ssh $CLIENT -l root "ps -ef | grep NO | grep -v grep"`
    NOWC=`ssh $CLIENT -l root "ps -ef | grep NO | grep -v grep | wc -l"`
            if [ $NOWC -gt 0 ] ; then
                    echo "There are $NOWC process(es) running"
                    echo "Here are the list of process(es)"
                    echo "$NO"
                    echo "Cannot find that process"
                    echo "Good Bye"
    exit 0
    I'm new to scripting and it is very challenging. I spent days trying to figure out why the $NOWC variable was returning a value of zero. I'm just glad that there are forums like this one that people are willing to help each other out. I really appreciate that. Thanks again the_ultimate_samurai and secondmouse for yalls help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts