Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have some code that controls a child process via open2. The sub process forks copies of itself to run in parallel, and might launch some subprocesses as well. I want to make sure that if the perl process ends, any remnent of the child (it's forks or its children) is gone. What I found putting "kill -15, $pid" didn't do the trick. Presumably, there is a a nice way to do clean up in situation like this -- at least better than the approach I am currently using.
END { if ($pid and (kill 0,$pid)) { system("pkill -TERM -g `ps --pid $pid --no-heading -o '%r'` "); # + Kill everything forked system("pkill -TERM -P $pid "); # Kill spawns kill("TERM", $pid); waitpid $pid, 0; } undef $pid; }
Thanks!

Replies are listed 'Best First'.
Re: No spawned process left behind
by zentara (Cardinal) on Dec 01, 2012 at 10:21 UTC