# How do I kill a spawned process?

**URL:** https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824
**Category:** General Usage
**Created:** [May 20, 2017, 6:26pm UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824 "2017-05-20T18:26:28Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [May 20, 2017, 6:26pm UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824/1 "2017-05-20T18:26:28Z")

</div>

Say I opened a video like this:

```julia
f = "path2video.file"
cmd = is_windows() ? `start $f` : is_linux() ? `xdg-open $f` : is_apple() ? `open $f` : error("Unknown OS")
run(cmd)

```

How can I kill this process from within Julia later on?

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [May 24, 2017, 5:31am UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824/2 "2017-05-24T05:31:34Z")

</div>

Since none of those commands return an handle to the launched process, the answer is “you can’t”

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [May 24, 2017, 7:08am UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824/3 "2017-05-24T07:08:12Z")

</div>

I think you can use `stream, proc = open(cmd)` and then call `kill(proc)`. Does that work for your situation?

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [May 24, 2017, 7:37am UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824/4 "2017-05-24T07:37:51Z")

</div>

> [@rdeits](#):
>
> stream, proc = open(cmd)

`kill(proc)` returns `0` but the (in this case) video-player stays open and doesn’t close, even when I exit Julia…

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [May 26, 2017, 5:37pm UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824/5 "2017-05-26T17:37:04Z")

</div>

I guess I wasn’t specific enough. Robin is correct that `open` will return a handle that you could later `kill`. However, the system programs listed (xdg-open, start, open) all fork again to run as daemons so that they cannot be killed (by design).

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [May 26, 2017, 7:21pm UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824/6 "2017-05-26T19:21:04Z")

</div>

ahhhh… OK, thanks…  
No way around that I guess? I’m using these to start a video of unknown (and often obscure) codec, but maybe there is a better way to do this…

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [May 27, 2017, 6:56am UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824/7 "2017-05-27T06:56:31Z")

</div>

How about using “xdg-mime query” to find the player, and then launching it directly?

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [May 29, 2017, 7:47am UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824/8 "2017-05-29T07:47:46Z")

</div>

> [@Per](#):
>
> xdg-mime query

That’s great for Linux, but I need this to work cross-platform. But you are right, I’ll search for how to find the default application for different file types across all three major platforms. Thanks!

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [June 1, 2017, 1:57pm UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824/9 "2017-06-01T13:57:14Z")

</div>

> [@Per](#):
>
> xdg-mime query

Wah…! This is pretty involved when it needs to be cross-platform. I’m starting to wonder if there isn’t a better solution to this problem.  
My main options are:

1. somehow identify which program runs which mime.
2. start playing videos by opening the files with the most obvious player per platform (`windows media player` for Windows).
3. somehow identify and kill processes that were spawned from `xdg-open $f` (and the Windows and Mac and Windows variations).

All three options have their disadvantages: #1 seems really hard on Windows, #2 is very vulnerable to errors (what if that ultra-strange codec file is played by some local proprietary program), #3 is apparently impossible.

I’d appreciate any insight you might have…!

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [June 1, 2017, 5:04pm UTC](https://discourse.julialang.org/t/how-do-i-kill-a-spawned-process/3824/10 "2017-06-01T17:04:50Z")

</div>

4: render to a webbrowser using HTTP.jl

Some example projects that might help get you started:  
[https://github.com/suan/instant-markdown-d](https://github.com/suan/instant-markdown-d)  
[https://github.com/Rykka/instant-rst.py](https://github.com/Rykka/instant-rst.py)
