What kill, kill -9, and kill -3 Mean in Linux and How They Differ

Many students often use the kill command to kill processes in daily Linux use. Some use kill directly, some use kill -9, some use kill -3. What do they mean and what's the difference? Today we'll learn about the kill command and signals in Linux.

Many students often use the kill command to kill processes in daily Linux use. Some use kill directly, some use kill -9, some use kill -3. What do they mean and what’s the difference? Today we’ll learn about the kill command and signals in Linux.

The kill Command

The format of the kill command is kill -Signal pid, where pid is the process number and signal is the signal sent to the process. By default, kill sends the SIGTERM (15) signal to the process, telling it it needs to be shut down and should stop running and exit on its own.

You can view the list of kill signals with the command kill -l:

kill -l command

Linux Signal

Linux supports POSIX standard signals and real-time signals. Below is a brief table of Linux signals:

SignalValueDefault ActionMeaning (reason for the signal)
SIGHUP1TermHangup of terminal or death of process
SIGINT2TermInterrupt signal from keyboard
SIGQUIT3CoreQuit signal from keyboard
SIGILL4CoreIllegal instruction
SIGABRT6CoreAbort exception signal
SIGFPE8CoreFloating-point exception
SIGKILL9TermKill
SIGSEGV11CoreSegmentation violation (invalid memory reference)
SIGPIPE13TermBroken pipe: write to a pipe with no reading process
SIGALRM14TermTimer expired from alarm
SIGTERM15TermTermination
SIGUSR130,10,16TermUser-defined signal 1
SIGUSR231,12,17TermUser-defined signal 2
SIGCHLD20,17,18IgnChild stopped or terminated
SIGCONT19,18,25ContContinue if stopped
SIGSTOP17,19,23StopStop signal not from terminal
SIGTSTP18,20,24StopStop signal from terminal
SIGTTIN21,21,26StopBackground process reads terminal
SIGTTOU22,22,27StopBackground process writes terminal
SIGBUS10,7,10CoreBus error (memory access error)
SIGPOLLTermPollable event occurred (Sys V), synonymous with SIGIO
SIGPROF27,27,29TermProfiling timer expired
SIGSYS12,-,12CoreInvalid system call (SVr4)
SIGTRAP5CoreTrace/breakpoint trap
SIGURG16,23,21IgnSocket urgent signal (4.2BSD)
SIGVTALRM26,26,28TermVirtual timer expired (4.2BSD)
SIGXCPU24,24,30CoreCPU time limit exceeded (4.2BSD)
SIGXFSZ25,25,31CoreFile size limit exceeded (4.2BSD)
SIGIOT6CoreIOT trap, synonymous with SIGABRT
SIGEMT7,-,7Term
SIGSTKFLT-,16,-TermCoprocessor stack error (unused)
SIGIO23,29,22TermI/O now possible on descriptor
SIGCLD-,-,18IgnSynonymous with SIGCHLD
SIGPWR29,30,19TermPower failure (System V)
SIGINFO29,-,-Synonymous with SIGPWR
SIGLOST-,-,-TermFile lock lost
SIGWINCH28,28,20IgnWindow size changed (4.3BSD, Sun)
SIGUNUSED-,31,-TermUnused signal (will be SIGSYS)

Some signal values are hardware-architecture dependent (generally alpha and sparc use the first value, i386, ppc, and sh use the middle value, mips uses the third value; - means the value is unknown for that architecture).

SIGKILL and SIGSTOP cannot be hooked, blocked, or ignored.

Before Linux 2.2 (inclusive), the default action of SIGSYS, SIGXCPU, SIGXFSZ, and SIGBUS (except on SPARC and MIPS) was to terminate the process but without a core dump. Linux 2.4 follows POSIX.1-2001, changing the default action of these signals to: terminate the process and also produce a core dump.

A process can change the default handling of a signal using the sigaction and signal system calls (using signal has poor portability). A process can choose one of the following three signal-handling methods:

  1. Perform the default action;
  2. Ignore the signal;
  3. Catch the signal, but call a custom handler via a signal handler.

A signal may be blocked. Each thread in a process has its own signal mask, indicating which signals of that thread are blocked. A thread sets its signal mask via pthread_sigmask. A single-threaded program can use sigprocmask to manipulate the signal mask. In a multi-threaded program, all threads share the same default behavior for handling a given signal.

The Difference Between kill, kill -9, and kill -3

After the above study, you now know the difference.

kill sends signal 15 by default, which is SIGTERM — telling the process it needs to be shut down and should stop running and exit on its own. The process can clean up caches and exit on its own, or refuse to exit.

kill -9 sends SIGKILL, meaning the process is terminated and must exit immediately — forcibly killing the process. This signal cannot be caught or ignored.

kill -3 prints the stack info of each thread of the process. After kill -3 pid, the file is saved at /proc/${pid}/cwd, with the filename antBuilderOutput.log.

Summary

If you want the process to run its own exit/cleanup routine, use the kill command, so the process can perform some cleanup and then exit. If the process is frozen and you need to record the scene of the accident, use kill -3 to record the accident-scene info and then exit. If you don’t need anything and just want to kill a process, use kill -9 — kill it violently.