kill: send signals to processes
kill sends a signal to a process. Most signals ask the process to do something, usually to stop. Despite the name, killing is only one of the things it can do.
The signals you will use
| Signal | Number | Meaning |
|---|---|---|
TERM | 15 | Please stop. The default; the program can clean up first. |
INT | 2 | Interrupt, the same as Ctrl+C. |
HUP | 1 | Hang-up. Many daemons reload their configuration on HUP. |
KILL | 9 | Stop immediately. Cannot be caught or ignored; no cleanup. |
STOP / CONT | 19 / 18 on Linux | Pause and resume a process. |
USR1, USR2 | 10, 12 on Linux | Program-defined, for example "reopen log files". |
Numbers differ between systems for some signals; names work everywhere. kill -l lists them.
Examples
kill 4122 # ask process 4122 to stop (TERM)
kill -HUP 1022 # ask nginx's master process to reload
kill -9 4122 # force, if it ignored TERM
kill -0 4122 && echo alive # does the process exist (and may I signal it)?
pkill -f "python3 report.py" # by command line
pkill -u anna # every process of a user
killall firefox # by name (Linux)
pgrep -a java # check what a pattern matches before using pkill
On System V Unix, killall is a shutdown command that kills every process. Use pkill on Solaris; on AIX use kill with PIDs from ps -ef.
Why kill -9 is the last resort
KILL stops a program in the middle of whatever it was doing: it cannot finish writing files, release locks, remove temporary files or tell its child processes. Databases may need recovery afterwards. Send TERM first, wait a few seconds, and only then use -9. A process in state D (see ps) may not die even from KILL until its disk or network operation finishes.
Services
For services started by systemd, use systemctl stop, restart or reload instead of kill, so systemd knows what happened and does not restart the service unexpectedly. See systemctl. To send a signal through systemd: systemctl kill -s HUP nginx.
Jobs in your shell
long-command & # run in the background
jobs # list background jobs of this shell
fg %1 # bring job 1 to the foreground
# Ctrl+Z pauses the foreground job, then:
bg # continue it in the background
kill %1 # signal a job by its job number
nohup ./backup.sh & # keep running after you log out (output in nohup.out)
disown %1 # remove a running job from the shell, so logging out does not stop it
For long work on remote servers, a terminal multiplexer is more convenient than nohup: see tmux.