Kill command is used to send a signal to a process or to kill a process. We typically use the command “kill -SIGNAL” to send the signal to a process.
# kill -[SIGNAL] [Process ID]
There are other ways to effectively kill a process — killing a process by name, killing a process by specifying part of the name, killing a process by pointing out the process with cursor etc. All the below kill conventions will send the TERM signal to the specified process. For the signals, either the signal name or signal number can be used. You need to look up the pid for the process and give it as an argument to kill.
$ kill -TERM pid $ kill -SIGTERM pid $ kill -15 pid
Available kill Signals
To get a list of all the available signals which one can send to a process using “kill -l”.
For example, the standard signal to send to a program to kill it is the SIGKILL signal, which has the signal ID 9. So, let’s first create a new process and then kill it; as an example, start a new sleep process in the background.
[root@geeklab ~]# sleep 1000 &  29553
Using ps with the option aux prints out the full command line:
# ps -aux | grep sleep root 29553 0.0 0.0 107948 352 pts/0 S 14:15 0:00 sleep 1000
Now, in order to kill this process, use kill -9 for sending the SIGKILL signal and then the process ID.
# kill -9 29553
Let’s confirm this using the ps command again:
# ps -aux | grep sleep
examples of killing a process
Several methods are available to kill a process, including the following:
- The kill command
- The pkill command
- The killall command
- The xkill command
1. kill command
Lets kill the firefox process using the kill command.
$ ps -ef | grep firefox 2003 ? Sl 7:22 /usr/lib/firefox-3.5.3/firefox
$ kill -9 2003
2. pkill command
With the pkill command, you can provide a process name, a username, or another method to indicate which process (or processes) to send a signal. So there is no need for you to find out the PID of the process to send the signal.
$ pkill sleep + Terminated sleep 1000
The following will send a kill signal to all processes owned by the user john:
$ pkill -u john
before you use the pkill command, use the “pgrep -l” command to view the process ID and process name of the matching processes.
# pgrep -l sleep 30597 sleep 30607 sleep 30621 sleep
# pkill sleep  Terminated sleep 1000 - Terminated sleep 2000 + Terminated sleep 3000
3. Killall Command
Instead of specifying a process by its PID, you can specify the name of the process. If more than one process runs with that name, all of them will be killed. To kill all the firefox processes in one go:
$ killall -9 firefox
4. xkill command
You can kill a process by running the xkill command and then just clicking the process that you want to stop. For example, as shown below I can close the Firefox application on the right by clicking anywhere on the Firefox window.
The first step in running the xkill command is to make sure you can see the window of the program you want to stop. In this case, the xkill command will be used to stop the Firefox window on the right side.
It is not a good idea to use “kill -9” always, Most programs require some sort of cleanup when it exits. These programs set up a signal handler to perform these cleanup duties as a result of SIGTERM and SIGINT. They would set up a signal handler for SIGKILL if they could, but SIGKILL (kill -9) is untrappable. Using SIGKILL don’t give an opportunity to clean up while exiting and don’t give an opportunity to delete any temporary files, shutdown sockets, remove shared memory segments, close open files, or some other task. This may corrupt database or other important temporary files.