LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using the process status (ps) command in a bash script (https://www.linuxquestions.org/questions/programming-9/using-the-process-status-ps-command-in-a-bash-script-728947/)

young_linux 05-27-2009 05:58 PM

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

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 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:


#!/bin/bash

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

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

exit"
done

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# ./test_ps5.sh 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
fi
echo Cannot' find that 'process
echo Good' 'Bye

exit'
Cannot find that process
Good Bye
+ exit 0
XX#



Any input would be appreciated and thank you in advance for your help!!

blacky_5251 05-27-2009 07:42 PM

I think your quoting is creating problems. Try this:-
Code:

#!/bin/bash

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

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

exit"
done

exit 0


chrism01 05-27-2009 08:49 PM

backquotes are wrong in orig, single quotes above prevent var interpolation; try this
Code:


#!/bin/bash

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

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

exit"
done

exit 0


young_linux 05-28-2009 01:24 PM

Thank you for your help and suggestions, but for some reason it is still not working. In Blacky's script; the $NOWC variable is still returning a value of zero. Here is the output of Blacky's script in debug mode:

Code:

XX# ./test_ps7.sh 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 0 processes running'\''
                                echo '\''Here is the list of processes'\''
                                eval ps -ef | grep NO | grep -v grep
                            fi
        echo '\''Cannot find that process'\''
        echo '\''Good Bye'\''

exit'
Cannot find that process
Good Bye
+ exit 0



In Chrism01's script the $CT variable has a value of 0. Here is the output of his script in debug mode:


Code:

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

Thank you so much in taking time out to post and helping me out. Please advise.

young_linux 05-29-2009 11:13 AM

Thanks again for yalls help. I finally got the script to work and here it is:

Code:

#!/bin/bash

for CLIENT in $*
do
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"
        else
                echo "Cannot find that process"
                echo "Good Bye"
        fi
done

exit 0

With the old script both NO and NOWC variable was returning a value that was on the host 0 because there was no process with NO. Adding the ssh command with the ps command inside the variable would allow me to grep for NO on the client.

Thanks again for your help chrism01 and blacky_5251.


All times are GMT -5. The time now is 02:14 AM.