问题
I have a Perl script that spawns some children. They all take quite a while to run, creating directories and files along the way. I often notice things I'd like to change before the children die of natural causes, so then I have to shut everything down (which involves a few grep
and kill
calls) and delete any files the children created. It's not really a big deal, but a bit of a pain in the neck. I'd like to create a setup where the children are all monitored so when I start up the parent again, old children still running are reported.
My best idea so far is keeping a log file of children I've run with their PIDs, checking it and updating it at the start of the parent script. Kill and report any children still running, and die to let the user clean up directories and files by hand before a fresh run.
So my question is this: How might I go about adding children to the log file? Is there a way to set up a trigger which could automatically take care of it, or am I stuck having to remember to do it any place in the code where a new process starts?
p.s. I'm certainly open to suggestions of better ways to accomplish this!
回答1:
How about signal handlers? The parent can track the pids of the jobs it has launched (and if you want, the pids that have been reaped with a SIGCHLD
handler). When you want to terminate everything prematurely, signal the parent to kill off all the children and clean up after them. If the child processes are also Perl scripts, you could put signal handlers in the children and see if they will clean up after themselves.
来源:https://stackoverflow.com/questions/5263169/keeping-track-children-from-old-runs-of-a-script