当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - Ensure orphaned processes are killed when the parent process dies

问题描述:

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.

网友答案:

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.
    • http://www.celeryproject.org/
    • http://aws.amazon.com/elasticbeanstalk/
      • More aggressive approach - basically spawn off whole OS instances but they'll definitely get killed off within your parameters for operation
  • 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.
分享给朋友:
您可能感兴趣的文章:
随机阅读: