How to Kill a Process in Linux

Exit Signals of Linux Processes

In order to understand process killing in Linux, we have to know how to commit this crime. In order to do a clean job, we need to understand the signals which Linux uses to send information between the kernel and a process or between processes. Following are the exit signals used in Linux processes:

  • NORMAL EXIT: Signal code is 0. It means that the process is closed normally.
  • SIGHUP: Signal code is 1. SIGHUP is the abbreviation of SIGNAL HANG UP, meaning that the signal does not mean that the process is killed, but hanged up. Often the signal is sent to the processes to reread its configuration file.
  • SIGTERM: Signal code is 15. As you guessed, it is the abbreviation of SIGNAL TERMINATE. SIGTERM causes the process to terminate as soon as possible. Do not expect the process to be closed immediately when you send the SIGTERM signal: the process tries to close all the connections, child processes, files etc. before closing down.
  • SIGKILL: Signal code is 9. SIGKILL tells the process to leave what it is doing and stop immediately.
  • SIGINT: Signal code is 2. SIGINT stands for SIGNAL INTERRUPT and as the name says, it does not kill a process; it just interrupts its execution. The command line equivalent of SIGINT is Ctrl + C.

How to Kill a Process in Linux

The command that is used to kill a process in Linux is the kill command. kill is one of the commands that does not have too many options. The following most-common options are available for kill:

  • n: n is an integer and denotes the process identifier (PID) of the process. The signal is sent only to the process with the PID.
  • -n: n is an integer and denotes the process group. The signal is sent to all of the processes in this process group.
  • -s: The signal to be sent to the process. s can be a signal name or number.
  • -l: Print a list of signal names.

To kill the process in Linux, we have to know the process ID of the process that we want to kill. We can get the PID of the application by the ps command together with the grep command, such as ps aux | grep firefox. This command will display various information about the Firefox process, including its PID; assume that it is 1572. To kill the Firefox process, we will use the kill command as:

kill 1572

if we want to force the process to be killed immediately, we will use

kill –SIGTERM 1572

or

kill -9 1572

We could also use killall command to end all the process tree of Firefox. In that case, the command would be:

killall Firefox

As you have just caught, killall takes process name as the argument, not the PID.

Do not use the killall command in UNIX! The usage and syntax is different.

The kill command comes especially handy if your system is responding slowly due to an application. Also, you can use the command to kill the entire desktop manager if your desktop becomes totally unresponsive. In this case, switch to another virtual session (hit Ctrl + Alt + F1), log on to the system and issue the killall gdm or killall kdm to end the Gnome or KDE desktop session.