[NTLUG:Discuss] PID of program started from bash?

Jack Snodgrass jack at jacksnodgrass.com
Wed Oct 13 20:32:18 CDT 2004


I did man bash... I might have even tried it, but couldn't figure it
out.... what's wrong with this test script? 

I first try and kill rhnsd so its not running.
I then ps -aef and verify that it's not running. 
I then start it.
I echo $!
I ps -aef to see it's pid
then I kill it... 

#!/bin/bash
killall -TERM rhnsd
echo "ps -aef | grep rhnsd "
ps -aef | grep rhnsd | grep -v grep
echo "starting..."
/usr/sbin/rhnsd
echo "$!"
echo "started..."
echo "ps -aef | grep rhnsd"
ps -aef | grep rhnsd | grep -v grep
killall -TERM rhnsd



as you can
see..                                                                                                                                                                              

I get:
./testit
rhnsd: no process killed
ps -aef | grep rhnsd
starting...
 
started...
ps -aef | grep rhnsd
root     12186     1  0 20:28 ?        00:00:00 /usr/sbin/rhnsd

.... no $! is printed.....
I tried 
echo $!
and 
echo "$!"

jack 



On Tue, 2004-10-12 at 23:56 -0500, Rusty Haddock wrote:
> Jack Snodgrass wrote:
>     >how do you get the PID of a program that you start from a bash script? 
> 
> C'mon Jack, man is your friend. :-)
> 
> 	man bash
> 
> Then, in the pager, search for \$ and you'll find the answer.
> To keep a short answer from getting too long, try
> 
> 	echo $!
> 
> It's like this in just 'bout all the "older" shells, like ksh, sh,
> and csh.
> 
> 	-Rusty-
-- 
Jack Snodgrass <jack at jacksnodgrass.com>




More information about the Discuss mailing list