2

In my Ruby app I'm trying to come up with a foolproof and cross-platform way of ensuring that a parent's child processes get killed when the parent does (see this question).

One possibility that I'm exploring is the idea of spawning a separate process that watches the parent and kills the child process if the parent dies. I've already come up with an effective way of letting this monitoring process know when the parent dies, but now I need a way for it to watch the child process so the monitoring process can exit if the child dies before the parent. Note that the "child process" is a child of the monitoring process's parent, not the monitoring process itself.

So what I need to know is this: Given the PID of some process, how can I check if that process is still running (or better yet, be notified when it dies) in a way that is cross-platform? (E.g. works on both Linux and Windows.)

Community
  • 1
  • 1
Ajedi32
  • 36,408
  • 19
  • 117
  • 155

1 Answers1

1

The easiest way is to test which operation system your ruby is running, and then through the command line use a simple test on linux or windows to check if your program is running.

The easiest way often is not the best way. Yet, be prepared to face some strange code, for example, how to simple kill a process on windows. It is your call.

I would love someone say I'm wrong and that there is a great way to manage processes in ruby being cross-platform. Please do.

Community
  • 1
  • 1
fotanus
  • 18,299
  • 11
  • 71
  • 106
  • Heh, me too. I did find a great library called [ChildProcess](https://github.com/jarib/childprocess) which does some of what I want, but it only seems to work with processes that it spawned; there's no way to wrap existing processes and then call methods like `alive?` on them. – Ajedi32 Aug 20 '13 at 21:07