Send `SIGINT` to `Child` on Unix

Child has the kill() method to send SIGKILL to a process.

Does anyone have a recommendation to send SIGINT or another signal to a process from Rust?

I couldn't find anything in std or on crates.io.

Based on the source for std, I think it would be pretty easy to implement myself, but I wanted to avoid that if there is an existing solution.

I reach for the nix crate any time I need something Unix specific. You might want to check it out.

1 Like

That crate was already included in my project :flushed:

I think my search for a solution was too specific.

Thanks!

Here is a gist with what I came up with. It requires the nix crate.

It attempts to stop the process gracefully using SIGINT, but if that fails or the timeout is reached, it will use Child::kill()

I'm not sure about the error handling because I don't know how I want to handle those in my application.

I'd welcome feedback anyone has about the error handling, code, or general approach.

I recommend using the kill binary.
It is the simplest solution I have found.
Just fetch the child's id and give it to the kill command:

Command::new("kill")
    .args(["-s", "INT", child.id().to_string()])
    .spawn()?;
kill.wait()?;