Click to See Complete Forum and Search --> : getting a PID in a bash script


NeilBlue
07-17-2002, 10:55 AM
Hello,

I am trying to write a bash script that will start a program in the background an save the new PID so that I can later kill the process off. Please could someone tell me how to get hold of the PID of the new process instead of the PID of the scipt which I can get with $$

thank you
Neil Blue

ph34r
07-17-2002, 11:04 AM
try

mypid=`pidof someprocessname`

echo $mypid > /var/lockfile.for.me


Then you can later do

pidtokill=`cat /var/lockfile.for.me`

kill -s 9 $pidtokill

NeilBlue
07-17-2002, 11:43 AM
thank you, but one silly question, how do I get the process name?

NeilBlue
07-17-2002, 11:45 AM
ah also, 'pidof' does not seem to be on my linux box.

X_console
07-17-2002, 11:53 AM
To get the PID of a process:

pid=$(ps -eo pid,fname | grep myProcess | awk '{print $1}')

NeilBlue
07-17-2002, 11:59 AM
thank you X_console,

that is what I have been using so far (good to know I am on the right track ), but I may have more than one process with the same name and I want to be selective about which one I kill of.

bwkaz
07-17-2002, 12:29 PM
Do you have source for the program you're trying to kill? If so, you could modify it to write its PID to a lock file.

If not... how do you normally decide which process to kill; can you emulate that reasoning in a script? Would it work to kill all of them anyway, or would this cause problems?

NeilBlue
07-17-2002, 12:53 PM
The process is tomcat, and I havn't solved the problem manually yet either.

furrycat
07-17-2002, 01:03 PM
The answer to the question is to use $!.

The answer to the problem is to wade through the pages of crap that are the tomcat startup scripts and find that you can get it to run in the foreground. Then you run it using Dan Bernstein's daemontools which you can get from http://cr.yp.to/daemontools.html

NeilBlue
07-18-2002, 04:10 AM
Thanks, that is working now
Cheers
Neil

cwolf
07-18-2002, 08:20 AM
You maybe want to read the pgrep manpage.