Need to monitor an existing external OS process

I need to monitor an external OS process and know when it exits. In other words, if I were in C, I’d use waitpid. (Unfortunately, google results are generally overwhelmed with articles about Erlang processes, not relevant to OS processes.) Some other details:

  • I’m not launching the process from Elixir
  • I’m not monitoring its stdout or stderr
  • I’m not sending data to it on stdin

Really, I have an OS pid, and all I want is to know immediately when that process exits. Is the way to do that to just System.cmd & a shell to wait?

Edit: shell won’t even work; it can only wait on its own children. Yeah, I’m remembering now–when I did this in C I did not use waitpid, I used kqueue…

1 Like

I have zero experience with this. But I was able to find erlexec which lists the following feature:

Ability to monitor the termination of OS processes

It also lists kqueue in the readme. I don’t know if its implementation assumes you have started the process (I.e. uses an Erlang port), but from grokking documentation it would seem it is more what you are interested in. It is also active so you may want to contact the author directly for any questions.

1 Like