Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need a script to check when process ends

Status
Not open for further replies.

Murugs

Technical User
Jun 24, 2002
549
US
I need a shell script which will perform the following.
If I do a
ps -u username | grep Python
25863 ? 0:00 Python
where Python is a process running on my machine.

I need to check whether the process Python is running on my machine.
When It ends I need to perform some other script.The script should run to check
whether still the process "Python" is existing or not.
I think it is a simple do while loop where I am not able to getting it work.
.

Post your responses please
Murugs
 
Try this

ps -u username | grep Python > /dev/null
status=$?

while [ $status -eq 0 ]
do
sleep 30
ps -u username | grep Python > /dev/null
status=$?
done

# call other script here ..

This will check every 30 seconds to see if the process is still running and will exit when it dissapears.
MB.
 
Hi butterfm

Your script was perfect and was exactly need of the hour.
But If possible shall you explain me each and every line of the script.
I am not able to understand status=$? etc..

Regards
Murugs
 
Hello.

Beware that 'grep Python' can find the 'grep Python' process itself. This depends of the precise timing of the commands.
This, of course, if the script is also run by 'username'.
Code:
ps -u username | grep Python
 25863 ?         0:00 Python    
 32783 ?         0:00 grep Python

I have seen somewhere that you could do
Code:
ps -u username | grep '[P]ython'
to avoid this problem.

 
Hi murugs,

butterfm's code checks whether the Python process is still living. Because we are not interested in the output of the ps command here, we can send it to the /dev/null device.
dchoulette's hint is very good (you don't need the quotes though) to avoid the output of the grep process itself!

status=$? assigns the exit status of the last command ($?) to the variable "status". An exit status of 0 means "successful". Therefore if the Python process is running then status=0 (otherwise status=1). If status=1 the while part is skipped and the next part of the script will be executed. If status=0 then the test [ $status -eq 0 ] (is status=0 ?) is true. Then the while loop is executed. The while loop checks the existence of the Python process every 30 sec. If the process has died the while loop is left and the next part of the script is executed.

Hope that helps,

mrjazz [pc2]

P.S. in the first line of any script you should insert #! /bin/ksh (assuming /bin/ksh is the right path) or whatever interpreter you like to execute your script.
 
Could someone explain why

ps -u username | grep [P]ython

works? In a case like this I've always done:

ps -u username | grep Python | grep -v grep

but it's certainly better to avoid the 2nd grep. My understanding has always been that square brackets are to let grep select from different possible letters.

Thanks, Chris
 
Hi CHoggarth,

What dchoulette did is very neat and is something i've not seen before. It's also not the easiest thing to explain but i'll try.

Then original grep command spawns off a process with the command "grep Python"

e.g.

ps -u username | grep Python
25863 ? 0:00 Python
32783 ? 0:00 grep Python

Using "grep [P]ython" spawns a process that has the command

grep [P]ython

e.g.

32783 ? 0:00 grep [P]ython

The pattern "grep [P]ython" isn't captured by the original command

ps -u username | grep [P]ython

becasue of the brackets in the string around the P.

e.g.

If we had a file blah.txt containing

Hello
[H]ello

And we ran
grep [H]ello blah.txt

We would only get "Hello" returned because [H]ello in the file doesn't match the pattern [H]ello in the grep command.

That must be the worst explanation anybody has ever posted on this site but hopefully you'll understand what I mean.

Cheers,
MB
 
MB

Thanks - I sort of understand, but ... how come "grep [P]ython" finds:

25863 ? 0:00 Python

but not:

32783 ? 0:00 grep Python

when they both contain the string "Python"? & if I create a file with lines:

0:00 Python
0:00 grep Python

The command finds them both.

I hasten to add that whatever the explanation I fully expect never to understand it!

All part of the charm of Unix I suppose :)




 
Hi,

It doens't find

32783 ? 0:00 grep Python

because it's not there. With the initail command containing the square brackets i.e.

ps -u username | grep [P]ython

then what is there is this :

32783 ? 0:00 grep [P]ython

This line will not be returned by the grep command because the square brackets in the command

ps -u username | grep [P]ython

are used for pattern matching whereas the square brackets in

32783 ? 0:00 grep [P]ython

are literal characters that are part of the string. The grep command basically says match only lines containing a word with the first letter in the set "P" and the remainder of the string being "ython". Only lines containg the word "Python" (and not [P]ython) will be returned

MB
 
MB - Of course, sorry, I didn't read your reply carefully enough. Thanks

Chris
 
MB :)

I've used that for ages without thinking about how it worked, thanks. Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top