Question

In Ruby, how do I ensure that child processes spawned from my program don't keep running when my main process exits or is killed?

Initially I thought I could just use at_exit in the main process, but that won't work if my main process gets kill -9ed or calls Kernel.exec. I need a solution that is (basically) foolproof, and cross-platform.

Was it helpful?

Solution

If you have to handle kill -9 termination for your parent app, then you have only a couple of choices that I can see:

  • Create a work queue manager and spawn/kill child processes from work queue manager. If you can't guarantee that the work queue manager won't also be killed without warning, then option 2 is your only choice I think, since the only thing you know for sure is that the child processes are still running.
  • Have the child processes check a "heartbeat" from the parent process through RPC or monitoring parent PID in memory or watching a date/time on keep-alive file in /tmp to make sure it's current.
    • If the child processes fail to see the parent processes doing it's job of either responding to RPC messages, staying in memory itself, or keeping a file date/time current the child processes must kill themselves.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top