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

**URL:** <https://discourse.julialang.org/t/how-to-run-a-process-in-background-but-still-close-it-on-exit/27231>\
**Category:** General Usage\
**Tags:** question, distributed\
**Created:** [August 6, 2019, 5:37pm UTC](https://discourse.julialang.org/t/how-to-run-a-process-in-background-but-still-close-it-on-exit/27231 "2019-08-06T17:37:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Post date:** [August 6, 2019, 5:37pm UTC](https://discourse.julialang.org/t/how-to-run-a-process-in-background-but-still-close-it-on-exit/27231/1 "2019-08-06T17:37:54Z")

</div>

In development for the [PProf.jl](https://github.com/vchuravy/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](https://discourse.julialang.org/t/ann-pprof-jl-explore-profiles-in-style/26198/4), and he’s added the functionality in a PR! 😊)

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:

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

```

vs

```julia
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! 🙂  
(And once we have good answers to those, I can add this info to the documentation: [Running External Programs · The Julia Language](https://docs.julialang.org/en/v1/manual/running-external-programs/#Running-External-Programs-1))

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [August 6, 2019, 5:52pm UTC](https://discourse.julialang.org/t/how-to-run-a-process-in-background-but-still-close-it-on-exit/27231/2 "2019-08-06T17:52:55Z")

</div>

what about `atexit(PProf.kill)`?

---

<div class="post-metadata">

**Author:** ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Post date:** [August 6, 2019, 7:36pm UTC](https://discourse.julialang.org/t/how-to-run-a-process-in-background-but-still-close-it-on-exit/27231/3 "2019-08-06T19:36:30Z")

</div>

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. 🙂 (or maybe deprecate one or the other).
