# Throw control-c to @spawn task?

**URL:** <https://discourse.julialang.org/t/throw-control-c-to-spawn-task/36656>\
**Category:** General Usage\
**Created:** [March 28, 2020, 9:34pm UTC](https://discourse.julialang.org/t/throw-control-c-to-spawn-task/36656 "2020-03-28T21:34:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)\
**Post date:** [March 28, 2020, 9:34pm UTC](https://discourse.julialang.org/t/throw-control-c-to-spawn-task/36656/1 "2020-03-28T21:34:00Z")

</div>

Is there a way to throw a control-c break to a task running as @spawn? The specific case is that, when running the SoX commandline music player, one sends control-c to tell the player to stop play, ie

```
p = @spawn(run(`play $args`))

```

and, now I might want to stop the music early.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [March 29, 2020, 12:03am UTC](https://discourse.julialang.org/t/throw-control-c-to-spawn-task/36656/2 "2020-03-29T00:03:25Z")

</div>

Making control-c work with `@spawn` is a [very hard problem](https://github.com/JuliaLang/julia/issues/33248).

But I don’t think you need `@spawn` here. Why not just use `p = run(`play $args`; wait=false)`? You can then `kill(p)`.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 29, 2020, 12:59am UTC](https://discourse.julialang.org/t/throw-control-c-to-spawn-task/36656/3 "2020-03-29T00:59:24Z")

</div>

`@spawn` or not, if you are talking about sending a `Control-C` to the subprocess you are actually talking about sending a `SIGINT` to the subprocess. Sending a interupt (however you interpret that) to the task you forked off won’t do anything.

In another word, all what you need is `kill(process, Base.SIGINT)` you just need to figure out a way to get tha `process` object and you can pick you way to do it whether you want to use `@spawn` or not.

---

<div class="post-metadata">

**Author:** ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)\
**Post date:** [March 29, 2020, 3:17am UTC](https://discourse.julialang.org/t/throw-control-c-to-spawn-task/36656/4 "2020-03-29T03:17:35Z")

</div>

I think I will use this, though what I would ideally like is what mimics on the command line:

pkill -STOP play  
pkill -CONT play

to pause and continue. But kill(p) will work, enough.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 29, 2020, 3:23am UTC](https://discourse.julialang.org/t/throw-control-c-to-spawn-task/36656/5 "2020-03-29T03:23:41Z")

</div>

You can change what signal you send

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [March 29, 2020, 3:25am UTC](https://discourse.julialang.org/t/throw-control-c-to-spawn-task/36656/6 "2020-03-29T03:25:31Z")

</div>

You can see that `kill` accept the second argument which defaults to `SIGTERM`: [https://docs.julialang.org/en/latest/base/base/#Base.kill-Tuple{Base.Process,Integer}](https://docs.julialang.org/en/latest/base/base/#Base.kill-Tuple%7BBase.Process,Integer%7D)

Julia’s `Base` does not define `SIGCONT` and `SIGSTOP` (not cross-platform?) but it looks like they are 18 and 19, respectively.

(Edit: oops @yuyichao was faster)

---

<div class="post-metadata">

**Author:** ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)\
**Post date:** [March 29, 2020, 7:22am UTC](https://discourse.julialang.org/t/throw-control-c-to-spawn-task/36656/7 "2020-03-29T07:22:38Z")

</div>

Ah, did not know that, thanks.
