LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2009, 05:58 PM   #1
young_linux
LQ Newbie
 
Registered: Feb 2009
Location: houston
Distribution: redhat and suse
Posts: 3

Rep: Reputation: 0
Question 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!!

Last edited by young_linux; 05-27-2009 at 07:51 PM. Reason: I had changed the variable $JOBNOWC to $NOWC in my original script. I made a mistake when copying it over to this post.
 
Old 05-27-2009, 07:42 PM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
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
 
Old 05-27-2009, 08:49 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 05-28-2009, 01:24 PM   #4
young_linux
LQ Newbie
 
Registered: Feb 2009
Location: houston
Distribution: redhat and suse
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 05-29-2009, 11:13 AM   #5
young_linux
LQ Newbie
 
Registered: Feb 2009
Location: houston
Distribution: redhat and suse
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

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.
 
  


Reply

Tags
command, process, ps, status



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
time a process within bash script aubrey-calm2 Linux - General 2 06-15-2007 01:39 PM
Bash command exit status humbletech99 Programming 12 07-06-2006 12:20 PM
Bash Script to Detect USB drive mount status nutthick Programming 6 02-02-2005 08:17 AM
how do i kill a process from inside a bash script? mikaelo Programming 4 05-28-2004 08:51 AM
ps (process status) command, PS_FORMAT how to set? alexander Linux - General 2 12-18-2002 03:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration