How to use kill on Unix systems
kill
is a Unix command that lets you send signals to processes, which is why the name "kill" is a bit misleading, as there are many signals that have nothing to do with terminating a process.
For security, a user can only send signals to processes which they own, unless they are the superuser.
You use the kill
command by supplying it with a process id of the process you wish to send the signal to and optionally a signal, in the form of a number or a name. The default signal used when no signal is specified is SIGTERM.
List signals
kill -l
lists all the signals on your OS. On my MacBook Pro with Mojave installed this is the output of running kill -l
.
- SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
- SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE
- SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS
- SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG
- SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD
- SIGTTIN 22) SIGTTOU 23) SIGIO 24) SIGXCPU
- SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
- SIGINFO 30) SIGUSR1 31) SIGUSR2
The mapping between numbers and signals can be different between Unix implementations.
Send a SIGTERM signal to a process
kill 64
will send a SIGTERM signal to the process with id 64. Notice how no signal number or name is specified. When that is the case, the default is to send the SIGTERM signal to the process.
Send a signal by number or name
To send a signal by number, type:
kill -9 64
.
This will send the SIGKILL (see number to signal mapping above) signal to process with id 64. The SIGKILL signal will cause a process to terminate immediately. While for other signals it is possible for the processes to register signal handlers, for instance, to do some clean up before terminating, SIGKILL kills the specified process right away.
SIGSTOP is similar in as that can't be handled with a signal handler.
To send a signal by name, type:kill -SIGQUIT 64
or kill -s SIGQUIT 64
This will send the, you guessed it, SIGQUIT signal to the process with id 64.
The SIGQUIT signal terminates the process and dumps the core before it does so.
Terminal signals
When you hit CTRL + C in your terminal, the signal sent to the current process is SIGINT. SIGINT asks the process to terminate.
For CTRL + Z it is SIGSTP. SIGSTP is the terminal equivalent to SIGSTOP. It suspends the process which can then be resumed at a later time with SIGCONT.