# How to ensure async subprocess ends with Julia?

**URL:** <https://discourse.julialang.org/t/how-to-ensure-async-subprocess-ends-with-julia/14317>\
**Category:** General Usage\
**Created:** [August 30, 2018, 1:25pm UTC](https://discourse.julialang.org/t/how-to-ensure-async-subprocess-ends-with-julia/14317 "2018-08-30T13:25:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sglyon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sglyon/32/67_2.png) [@sglyon](https://discourse.julialang.org/u/sglyon)\
**Post date:** [August 30, 2018, 1:25pm UTC](https://discourse.julialang.org/t/how-to-ensure-async-subprocess-ends-with-julia/14317/1 "2018-08-30T13:25:18Z")

</div>

Suppose I do something like this

```julia
proc = open(`long_running_command`)

```

How can I ensure that the process exits when Julia does?

If I just `open` it, the process will outlive Julia.

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [August 30, 2018, 2:04pm UTC](https://discourse.julialang.org/t/how-to-ensure-async-subprocess-ends-with-julia/14317/2 "2018-08-30T14:04:56Z")

</div>

Is the process something you can communicate with to signal it should shut down itself, or are you looking to just kill it?

---

<div class="post-metadata">

**Author:** ![sglyon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sglyon/32/67_2.png) [@sglyon](https://discourse.julialang.org/u/sglyon)\
**Post date:** [August 30, 2018, 2:11pm UTC](https://discourse.julialang.org/t/how-to-ensure-async-subprocess-ends-with-julia/14317/3 "2018-08-30T14:11:33Z")

</div>

Just need to kill it.

The process starts a webserver that I send requests to, but doesn’t have any state that requires it to shut down gracefully.

Something like `@at_julia_end kill(proc)` would be great if `@at_julia_end` registers the argument to be run at shutdown

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 30, 2018, 2:25pm UTC](https://discourse.julialang.org/t/how-to-ensure-async-subprocess-ends-with-julia/14317/4 "2018-08-30T14:25:01Z")

</div>

Either of these should work:

```julia
help?> atexit
search: atexit CapturedException InvalidStateException

  atexit(f)

  Register a zero-argument function f() to be called at process exit. atexit() hooks are called in last in first out (LIFO) order and run before object finalizers.

help?> finalizer
search: finalizer UndefInitializer finalize

  finalizer(f, x)

  Register a function f(x) to be called when there are no program-accessible references to x, and return x. The type of x must be a mutable struct, otherwise the behavior of this function is unpredictable.

```

You’d have to register a function that’s manually killing the process you created though.

---

<div class="post-metadata">

**Author:** ![sglyon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sglyon/32/67_2.png) [@sglyon](https://discourse.julialang.org/u/sglyon)\
**Post date:** [August 30, 2018, 2:57pm UTC](https://discourse.julialang.org/t/how-to-ensure-async-subprocess-ends-with-julia/14317/5 "2018-08-30T14:57:25Z")

</div>

Excellent, thank you!
