Linux Kill Command Examples

Howto kill processes in Linux

Terminating running Processes in Linux


Processes are basically programs that are executing. Every process in Linux has what is know as a unique Process ID or "PID" for short. Any process that has been spawned from another process has an associated Parent Process ID "PPID". For an explanation of processes visit our process page. In most cases processes will run and terminate by their selves, however, this is not always the case. Occasionally a program may become unresponsive and refuse to terminate or may even start to consume large amounts of Memory or CPU. When this happens you may want to terminate these processes. In Linux there are numerous commands for identifying processes. We will cover some of the more popular methods of identifying processes that are running on your system and how to then terminate such processes.



Top


Top is probably one of the most common used commands to quickly identify processes that are running and what resources they are consuming. To demonstrate "top", I am going to run a little script that will consume high amounts of CPU. This will be easy to spot in "top". We will then identify the "PID" - Process ID and then kill it from the interactive menu screen. The simple script I shall run is is follows:



#!/bin/bash
#This will cause the cat command to consume CPU :-(

CORES=1

for i in `seq 1 $CORES`
 do cat /dev/zero > /dev/null &
done

So lets run the above script and take a look at the output from the top interactive screen:


top kill process


Here we can see that our script has a PID of "4235" and that it is consuming a large amount of CPU. We can also see that the command that is running is "cat". To kill this process, we can simply press the letter "k" on our keyboard. You will then be prompted to supply are PID to kill "PID to kill:".


top kill process


Once you have responded with the correct PID, you will then be asked to confirm Kill PID 4235 with signal [15]:

In this case we are going to accept the default signal value of "15" by pressing enter. Our process should now be terminated.



kill command and Signals Explained


When you issue the "kill" command from either a command line or an interactive tool such as "top" you actually send a signal value. The default value is "15". These numbers correspond to specific signals which are used by the system for handling processes.

The syntax for the kill command is: kill signal or option PID(s)

In our example we could have issued the command "kill 4235" This would have sent the signal "15" meaning "SIGTERM" to the process. If no signal is supplied after the "kill" command, then the default value of "15" is used.

We could have issued the command "kill -9 4235". The "-9" signifies the signal "SIGKILL". Normally you would only use the "-9" signal as a last resort as this will almost guarantee termination of the process. In most cases the default value should we sufficient to terminate your process.

For a full list of all the signals that are available to your system, you can issue the command "kill -l".

Example: kill -l under Ubuntu Linux



john@john-desktop:~/example$ kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX

As you can see there are numerous signals that you can use with the kill command.



Frequently Used kill options


kill -9 -1 : Kill all processes you can kill (processes owned by you).

kill -l 9 : Translate number 9 into a signal name. (In this case KILL).

kill 246 810 1214 1618 : Send the default signal, SIGTERM, to all those processes specified by their PID.



ps - The Process Display Command


The "ps" command reports information relating to a process. Here we can identify the PID and the associated command that is running with that PID:

To see every process on the system using standard syntax:



          ps -e
          ps -ef
          ps -eF
          ps -ely

To see every process on the system using BSD syntax:



          ps ax
          ps axu

To print a process tree:



          ps -ejH
          ps axjf


ps Command Examples



john@john-desktop:~/example$ ps
  PID TTY          TIME CMD
 3818 pts/0    00:00:00 bash
 4737 pts/0    00:00:00 ps


john@john-desktop:~/example$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 20:20 ?        00:00:00 /sbin/init
root         2     0  0 20:20 ?        00:00:00 [kthreadd]
root         3     2  0 20:20 ?        00:00:00 [ksoftirqd/0]
root         5     2  0 20:20 ?        00:00:00 [kworker/u:0]
root         6     2  0 20:20 ?        00:00:00 [migration/0]
root         7     2  0 20:20 ?        00:00:00 [watchdog/0]
root         8     2  0 20:20 ?        00:00:00 [cpuset]
root         9     2  0 20:20 ?        00:00:00 [khelper]
root        10     2  0 20:20 ?        00:00:00 [kdevtmpfs]


Search for high CPU consuming processes


Another way to display processes that are consuming high cpu is to use the "ps" command combined with the "sort" command:



john@john-desktop:~/example$ ps aux | sort -k 3,3 | tail -n 5
john      4640 31.0  0.0   4484   540 pts/0    R    20:48   3:47 cat /dev/zero
john      4643 31.0  0.0   4484   544 pts/0    R    20:48   3:44 cat /dev/zero
john      4637 31.4  0.0   4484   544 pts/0    R    20:48   3:50 cat /dev/zero
john      3928  3.4  4.1 558976 105880 ?       Sl   20:30   1:03 /opt/google/chrome/chrome 

In the above example we are piping the output from the "ps aux" command into sort. We then sort on the third column which is "CPU%". To limit our output to a handful of lines we use the "tail"command.



Searching for a process by name


The following example illustrates a "ps" command used with the "grep" command. In this example, we are locating the process called "gedit"



john@john-desktop:~/example$ ps -ef | grep gedit
john      3792     1  1 21:16 ?        00:02:10 gedit
john      4758  3818  0 23:06 pts/0    00:00:00 grep --color=auto gedit

Notice, there are two processes found, one is the actual process "gedit" with a PID of "3792". The other process is our grep command. If we wanted to exclude the grep process from being included in the results, we can use the "-v" option:



john@john-desktop:~/example$ ps -ef | grep -v grep | grep edit
john      3792     1  1 21:16 ?        00:02:14 gedit

We can see from the above output that our process id (PID) is "3792". If we wanted to terminate this process we would issue the command "kill 3792"



pgrep


pgrep is a command that can be sued for looking up a process (PID). As we saw from the previous example, we used the "ps" command with "grep" to narrow our search down. "pgrep" can do this in a lot simpler format

pgrep syntax: pgrep -u user name of process

In this example, I will search for the process name "gedit" that is running with the user "john"



john@john-desktop:~/example$ pgrep -u john gedit
3792


killall - Kill All Processes by name


The command "killall" sends a signal to all processes running any of the specified commands to terminate. If no signal is specified, then "SIGTERM" is sent by default. This command is useful if you wish to kill multiple processes with the same name.

If we wanted to kill the three processes running the "cat" command as per example below, we could issue the command: "kill 4637 4640 4643", however, we could use the "killall" command and issue "killall cat



  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
 4637 john      20   0  4484  544  456 R 30.6  0.0   2:47.84 cat                
 4640 john      20   0  4484  540  456 R 30.6  0.0   2:44.37 cat                
 4643 john      20   0  4484  544  456 R 30.6  0.0   2:42.12 cat                
 1198 root      20   0 92584  16m 5392 S  2.6  0.6   1:12.39 Xorg    


john@john-desktop:~/example$ ps aux | sort -k 3,3 | tail -n 5
john      3928  2.8  4.1 558976 106480 ?       Sl   20:30   1:06 /opt/google/chrome/chrome       
john      4640 30.8  0.0   4484   540 pts/0    R    20:48   6:26 cat /dev/zero
john      4643 30.8  0.0   4484   544 pts/0    R    20:48   6:24 cat /dev/zero
john      4637 31.0  0.0   4484   544 pts/0    R    20:48   6:29 cat /dev/zero
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

john@john-desktop:~/example$ killall cat

john@john-desktop:~/example$ ps aux | sort -k 3,3 | tail -n 5
john      4548  1.4  1.0 188948 26804 ?        Sl   20:41   0:24 gedit
john      1962  1.8  2.2 279096 57604 ?        Sl   20:17   0:58 compiz
root      1198  2.6  0.6  25624 16480 tty7     Ss+  20:16   1:24 /usr/bin/X :0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch -background none
john      3928  2.8  4.1 558976 106480 ?       Sl   20:30   1:06 /opt/google/chrome/chrome       

From the above output we have used our ps command to sort the highest users of CPU. In this example I am running our CPU script from the earlier example which causes the "cat" command to consume large amounts of "CPU". Next we issued the "killall cat" command and then issued our "ps aux | sort -k 3,3 | tail -n 5" command again to display the highest users of CPU. We can now see that these processes have gone.