Throw control-c to @spawn task?

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.

Making control-c work with @spawn is a very hard problem.

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

1 Like

@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.

1 Like

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§ will work, enough.

You can change what signal you send

1 Like

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}

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)

2 Likes

Ah, did not know that, thanks.