How to run a Process in background but still close it on exit?

In development for the PProf.jl package, we want to spawn a child process (pprof -web ...), and have it run in the background, so that you can keep using julia with the webserver open. (This suggestion came from @gdkrmr, in a discourse post, and he’s added the functionality in a PR! :blush:)

I originally suggested to use p = run(`...`, wait = false), which does indeed open the process in the background, and allow you to later kill it and restart it.

However, it appears that wait=false also detaches the process, so that when you kill Julia, the process remains open. I can’t find any documentation confirming that, but it seems like it from my experience.

After some experimentation, I think this is the right way to open a process in the background without detaching:

p = open(pipeline(`...`))  # open process in background, kill if julia exits.

vs

p = run(`...`, wait=false)  # open process in background, and detach, so it stays alive if julia exits.

My questions:

  1. Am I right about the difference between these two?
  2. Are there any other differences besides this difference that I should know about?

Thanks! :slightly_smiling_face:
(And once we have good answers to those, I can add this info to the documentation: Running External Programs · The Julia Language)

1 Like

what about atexit(PProf.kill)?

1 Like

I’m not sure. If the process is force-killed (like via kill -9) i don’t think the atexit hooks have a chance to run.

I think the default behavior for child-processes is to be killed when the parent is killed, so i think wait=false must be explicitly detaching the process, and open(pipeline(...)) is not? I’m hoping we can confirm that and then document the two options and their distinction in the docs. :slight_smile: (or maybe deprecate one or the other).

1 Like