[NCLUG] Bashing lost children
Michael Dwyer
mdwyer at sixthdimension.com
Wed Apr 10 14:53:36 MDT 2002
Tkil wrote:
>
> >>>>> "Michael" == Michael Dwyer <mdwyer at sixthdimension.com> writes:
>
> Michael> Well... except in a case like this:
>
> Michael> sleep 30 | logger &
> Michael> echo $! >/var/run/sleep.pid
>
> Michael> it stores the PID of 'logger' instead of sleep... Argh.
>
> the first solution that comes to mind is something like this:
>
> | $ perl -we 'open(PID, ">/var/run/$ARGV[0].pid") &&
> | print(PID "$$\n") &&
> | close(PID) &&
> | exec(@ARGV);' sleep 20 | logger &
Well, I almost pulled out my old "build your own shell" homework to
solve this one... but I found a slightly more elegant solution -- use
'ash' instead of 'bash'. Ash has a 'jobid' builtin that is a little
better behaved than the $! construct. It outputs the PIDs of all the
immediate children -- both sleep and logger! On top of that, I save a
little memory, too! Yay!
Thanks again for your help... Your answers were well above and beyond
the call!
More information about the NCLUG
mailing list