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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the PID for a process started in a shell script? 2

Status
Not open for further replies.

octopus

Programmer
Aug 9, 2000
3
SE
I want to get the PID for a process started in shell script. <br><br>Example: I start a program named ´testprog´. I want to save the PID for that process in a file so I can kill that process later. Because I want to start several instances of ´testprog´ but not kill all of them at the same time, I can't grep the process names and extract PID's for them in that way.
 
The following is from one of my scripts used to shutdown one of our applications.&nbsp;&nbsp;It finds the pid and creates the variable, later in the script it issues a kill on that variable.<br><br><i>pid=`ps -ef ¦grep <b>processname</b> ¦grep <b>processowner</b> ¦grep -v grep ¦cut -c10-15`<br>if [ -z &quot;$pid&quot; ]<br>then<br>&nbsp;&nbsp;echo Warning: mmscon not found.<br>fi</i><br><br>yada yada more script here...<br><br><i>kill $pid</i><br><br>To check to ensure it's gone try this<br><br><i>if [ -n &quot;$pid&quot; ]<br>then<br>&nbsp;&nbsp;pid=`ps -ef ¦grep <b>processname</b> ¦grep <b>processowner</b> ¦grep -v grep ¦cut -c10-15`<br>&nbsp;&nbsp;if [ -z &quot;$pid&quot; ]<br>&nbsp;&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;echo <b>processname</b> stopped.<br>&nbsp;&nbsp;fi<br>fi</i><br><br>processname = (in your case) testprog<br>processowner= name of user that ran the process.<br><br>Hope this helps <p>Jon Zimmer<br><a href=mailto:b0rg@pcgeek.net>b0rg@pcgeek.net</a><br><a href= Aetea Information Technology</a><br>The software required `Windows 95 or better', so I installed Linux.<br>
 
BTW the above is from a Korn shell script. <p>Jon Zimmer<br><a href=mailto:b0rg@pcgeek.net>b0rg@pcgeek.net</a><br><a href= Aetea Information Technology</a><br>The software required `Windows 95 or better', so I installed Linux.<br>
 
Well, thanks for the example but that's exactly what I don't want to do. The script you wrote kills <i>all</i> processes named <i>processname</i> owned by <i>processowner</i>.<br><br>I want to get the actual pid value for the started process.<br><br>&nbsp;&nbsp;/Octopus
 
try: -<br><br>pid=`ps -ef ¦grep processname ¦grep processowner ¦grep -v grep ¦ sort +4 -r ¦ tail -1 ¦cut -c10-15`<br> <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
The <b>$$</b> environment variable contains the PID of the current process.&nbsp;&nbsp;You could alter &quot;testprog&quot; so that it sends the value of $$ to a file.&nbsp;&nbsp;A related variable is <b>$!</b> - this contains the PID of the last background process run from the current shell. <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
Good one Andy <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
I personnally find $! much more useful than $$. This allows me to record automatically the pid of a process that I start in a script, so that later I can check the status or kill that process, referring to its pid.

As in perl the $! has another meaning, (this variable is basically the last error message), it's been quite hard to find, until I found this thread, so thanks a lot, AndyBo!

In perl this can be used this way:

Code:
#!/usr/bin/perl
system &quot;sleep 60 &
echo \$! > test.log&quot;;  # the \ makes it interprete $! as a                       
                 # shell variable, and not as the 
                 # perl $! variable
and you get the pid of &quot;sleep 60&quot; recorded in the file test.log
With $$ you get the pid of the script itself, which I personnally have no use for.

In shell scripting:
Code:
#!/bin/bash
sleep 60 &
echo $! > test.log

Jacques
 
hi guys....
can you help me quickly?

i've got korn shell on a windows box. i need to reset the priority of one of the threads. any clue how to do that. furthermore, how can i see all available commands that korn shell offers?

thanks a lot

zimmerli
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top