kill
counterintuitively, you can use kill
to send *any* signal,
not just signals which kill the process. For example:
$ kill -STOP 1234
will send a SIGSTOP
signal to PID 1234. The system call that's used to send signals is also called kill
.
to send a signal, you need to be running as the same user (or root). Otherwise, you'd be able to kill processes run by root, which would be bad!
kill PID
on it?
kill
without any argument sends the SIGTERM
signal. The default behavior for processes when they get a
SIGTERM
is to exit immediately.
kill PID
?
any process can set signal handlers functions for a given signal (like
SIGINT
). You can either use a predefined signal disposition (like
SIG_IGN
for "ignore") or run a custom function.
Ctrl+C
in a terminal terminate the foreground process?
when you press Ctrl+C
in your terminal, the kernel
translates that into sending a SIGINT
signal to the
running process
kill -9 PID
, is it possible for the process
to choose to handle it another way than just exiting immediately?
kill -9
sends the SIGKILL
signal, which can't
be blocked. It's a good way to force a process to exit immediately.
SIGSTOP
can't be blocked either.
kill -9
guaranteed to always kill a process?
processes can't block being killed with SIGKILL
by setting
a signal handler, but sometimes on Linux they get stuck doing I/O or something and become
impossible to kill. This unkillable state is called "uninterruptable sleep".
by default, signals don't wait for anything -- they just interrupt the process at a completely arbitrary point in its execution.
If you'd rather be interrupted only when it's convenient for the
process, you can set the process's signal mask to block signals and
then run sigwait
when you want to receive them
kill PID
send the same signal as pressing Ctrl+C
in a terminal?
kill PID
sends a SIGTERM
and
Ctrl+C
sends a SIGINT
SIGSTOP
) cause the process to exit?
stopping the process is like hitting "pause" -- it stops using the CPU but all of the process's current state stays intact. You can resume it later and it'll pick up right where it left off.
if you have a program (like vim
) running in a terminal, you can send SIGTSTP
(which will also stop the process by default) to it by pressing Ctrl+Z
SIGUSR1
, SIGUSR2
, and SIGHUP
terminate the process by default, but processes sometimes use these
signals to reload configuration or print out debug info. For example,
if you send nginx a SIGHUP
, it'll reload its
configuration.