[NCLUG] Bashing lost children

Michael Dwyer mdwyer at sixthdimension.com
Tue Apr 9 16:54:12 MDT 2002


No, no!  Put away the pitch-forks and oil-soaked-rag torches!!  I'm
talking about child /processes/ in the bash shell!

Okay, so you know how the shell variable $$ stands for the PID of the
shell?  Great.  Now, what if you want to keep track of the PID of a
process that the shell spawns.  The shell is aware of the PID, since it
echos it on the terminal:

  /bin/sleep 15 &
  [1] 30631

I suppose I could even use some fancy AWK or SED to pull it out of jobs:

  jobs -l | awk '{print $2;}' -
  30631

But this seems strange to me -- isn't this PID stored somewhere useful? 
Or passed back somehow?  The script below sort of explains what I want
to do...  Am I missing something horribly obvious?

#!/bin/bash
#
# This line stores the pid of the bash shell.
echo $$ > /var/run/bash.pid

# Now... how do I get to the pid of the child that I create?
/bin/sleep 100 &
CHILD_PID=???????
echo $CHILD_PID >/var/run/sleep.pid
wait $CHILD_PID

rm /var/run/bash.pid
rm /var/run/sleep.pid



More information about the NCLUG mailing list