Linux kill command tutorial

Linux kill command sends signal to a Linux process. Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be specified in three ways: -9, -SIGKILL, -KILL. That's what you can read from the Linux kill command manual page. If you don't quite understand what those descriptions are, here is a simple explanation for you. Every Linux user needs to learn the Linux kill command because it is particularly used to terminate Linux process(Linux process or Linux daemon). It is the same as the Windows task manager, where it is used to terminate a hanged program, zombie or any process that we don't need.

The Linux kill command synopsis:

kill [ -signal | -s signal ] pid ...
kill [ -L | -V, --version ]
kill -l [ signal ]

The Linux kill -L and -V options give invalid signal specification in Ubuntu. It doesn't matter, we do not need them anyway.

We can use '-l' option to check signal that kill command can sends to a process. Here are some examples of kill -l option command:

root@kkcjlab-server:~# kill -l 1
root@kkcjlab-server:~# kill -l 2
root@ubuntu-server:~# kill -l 3
root@ubuntu-server:~# kill -l 4
root@ubuntu-server:~# kill -l 5
root@ubuntu-server:~# kill -l 6
root@ubuntu-server:~# kill -l 7
root@ubuntu-server:~# kill -l 8
root@ubuntu-server:~# kill -l 9
root@ubuntu-server:~# kill -l 0

The most important Linux kill command syntax is the 'kill [ -signal | -s signal ] pid ...' where you can use the kill command to send the signal to the Linux process. The 'pid' in the command syntax is the 'process id' of the Linux process which we want to send the signal to. Here we need a little help from the Linux ps command. You can learn more about Linux ps command from Introduction to Linux process and daemons tutorial.

To understand how to use Linux kill command to terminate unwanted process, let's see a step by step example. First, open a new command line terminal, for example the tty2. Login as a normal user and run 'w3m' at the command prompt to open a web browser in Linux command line terminal.

Now switch back to terminal 1 or whatever terminal you originally login from and type 'ps aux | grep tty2' at the command prompt. See example command and result below:

root@ubuntu-server:~# ps aux | grep tty2
root 2197 0.0 0.0 3028 1616 tty2 Ss 08:23 0:00 /bin/login --
jimi 14088 0.0 0.0 5884 3160 tty2 S 16:09 0:00 -bash
jimi 14424 0.0 0.1 8360 4300 tty2 S+ 16:10 0:00 w3m
root 14552 0.0 0.0 3340 808 pts/0 S+ 16:11 0:00 grep tty2

The most important thing here is the second column which is the pid column. Now that we already know the pid, we can send signal using kill command to the process that we want to kill. To send a KILL signal to a process, we can use signal 9 (see kill -l example above). Let's say we need to terminate w3m process from tty2 which is hang, for example. From the ps result above, we can see that the w3m pid is 14424. The complete command to terminate the process is 'kill 9 14424'. See example below:

root@kkcjlab-server:~# kill 9 14424
root@kkcjlab-server:~# ps aux | grep tty2
root 2197 0.0 0.0 3028 1616 tty2 Ss 08:23 0:00 /bin/login --
jimi 14088 0.0 0.0 5884 3164 tty2 S+ 16:09 0:00 -bash
root 14909 0.0 0.0 3340 808 pts/0 S+ 16:22 0:00 grep tty2

If you want to see more examples of Linux kill command in action, the Stop and terminate hang program in Linux tutorial shows some simple real life examples.

Add new comment