Child::kill
sends a SIGKILL
, but how can I send any other signal such as SIGTERM
? I can probably use libc
and its signal API, but is there a better way to do this?
fork() creates the child process from the parent. The pid can be checked to decide whether it is the child (if pid == 0) or the parent (pid = child process id). The parent can then send messages to child using the pid and kill(). The child picks up these signals with signal() and calls appropriate functions.
a child process inherits signal settings from its parent during fork (). When process performs exec (), previously ignored signals remain ignored but installed handlers are set back to the default handler.
There is no automatic propagation of signals (SIGTERM or otherwise) to children in the process tree.
A child process in computing is a process created by another process (the parent process). This technique pertains to multitasking operating systems, and is sometimes called a subprocess or traditionally a subtask.
The nix
library does a good job of providing idiomatic rust wrappers around low-level UNIX operations, including sending and handling signals. In this case, you would create a nix::Pid
from child_process.id()
, then pass it to kill
like so:
use nix::unistd::Pid;
use nix::sys::signal::{self, Signal};
// Spawn child process.
let mut child = std::process::Command::new();
/* build rest of command */
child.spawn().unwrap();
// Send SIGTERM to child process.
signal::kill(Pid::from_raw(child.id()), Signal::SIGTERM).unwrap();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With