Kill command in UNIX and Linux is normally used to kill a suspended or hanged process or process group. Though kill is mainly associated with kill operation its mere a signal transporter and can send specified signal to specified process in UNIX or UNIX like systems e.g. Linux, Solaris or FreeBSD. Like in windows when we see a particular process hung the system we go to task-manager find the process and kill it, similarly in UNIX and Linux we first find the process ID (PID) of offending process and then kill it. Though we have killAll command also which doesn't require PID instead it can kill the process with just process name. Kill commands is often a wrapper around kill () system call but some Linux systems also has built-in kill in place. In this article we will see some examples of kill command in UNIX and how we can use kill command to kill the locked process.
This UNIX command tutorial is in continuation of 10 Example of Sort command in UNIX and 10 Example of tar command in UNIX and Linux and 10 Examples of VI editor in UNIX.
Kill command examples in UNIX and Linux
As I said earlier kill sends signals to specified process and it can send all signals specified in . Here we will see some examples of kill command in UNIX and Linux:1) Kill command to forcefully kill a process in UNIX
kill -9 is used to forcefully terminate a process in Unix. Here is syntax of kill command in UNIX.
ps -ef| grep process_identifier // will give you PID
kill -9 PID
2) Unix kills command to kill multiple processes
With kill command in UNIX you can specify multiple PID at same time and all process will be signaled or if signal is KILL they get killed like below kill command in UNIX
Syntax of kill in UNIX for killing multiple processes:
kill -9 pid1 pid 2
Here is an example of killing multiple processes in UNIX:
trader@asia:/ ps -ef
UID PID PPID TTY STIME COMMAND
trader 5736 5332 1 Nov 14 /usr/bin/bash
trader 5604 5552 0 Nov 16 /usr/bin/bash
trader 3508 4872 2 Nov 17 /usr/bin/bash
trader 6532 5604 0 17:43:19 /usr/bin/man
trader 6352 3420 0 17:43:22 /usr/bin/sh
trader 7432 6352 0 17:43:22 /usr/bin/less
trader 5348 3508 2 17:52:59 /usr/bin/ps
trader@asia:/ kill -9 3420 6352
trader@asia:/ ps -ef
UID PID PPID TTY STIME COMMAND
trader 5736 5332 1 Nov 14 /usr/bin/bash
trader 5604 5552 0 Nov 16 /usr/bin/bash
trader 3508 4872 2 Nov 17 /usr/bin/bash
trader 5040 3508 2 17:53:38 /usr/bin/ps
3) Kill command in UNIX to find Signal name
Kill command can also show you name of Signal if you rung it with option "-l". For example "9" is KILL signal while "3" is QUIT signal.
trader@asia:/ kill -l 3
QUIT
trader@asia:/ kill -l 9
KILL
4) Printing all signals supported by kill in UNIX
You can use kill -l to list down all signals supported by kill command in UNIX as shown in below example:
trader:~ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGEMT 8) SIGFPE 9) SIGKILL 10) SIGBUS
11) SIGSEGV 12) SIGSYS 13) SIGPIPE
5) Sending signals using -s option of kill command in UNIX.
Instead of specifying number you can specify name of signal you are sending to other process with kill command option "-s". Here is an example of using Kill command in UNIX with signal code.
trader:~ ps -ef
UID PID PPID TTY STIME COMMAND
trader 5736 5332 1 Nov 14 /usr/bin/bash
trader 3508 1 2 Nov 17 /usr/bin/bash
trader 7528 2352 0 18:00:30 /usr/bin/bash
trader 4424 7528 0 18:05:11 /usr/bin/less
trader 168 7528 0 18:05:15 /usr/bin/ps
[1]+ Stopped less -r a
trader:~ kill -s KILL 4424
trader:~ ps -ef
UID PID PPID TTY STIME COMMAND
trader 5736 5332 1 Nov 14 /usr/bin/bash
trader 3508 1 2 Nov 17 /usr/bin/bash
trader 7528 2352 0 18:00:30 /usr/bin/bash
trader 5044 7528 0 18:05:32 /usr/bin/ps
[1]+ Killed less -r a
Important point about kill command in UNIX and Linux
To summarize discussion and examples of UNIX kill command, I have outlined some of the important points and things to remember related to kill command in UNIX and Linux. You can quickly refer this point whenever you have some doubt over kill in UNIX.
1) Kill command in UNIX can send signals to any other process in UNIX or Linux.In order to work with those signals corresponding process should understand those signals.
2) You can get full list of signals supported by kill command in unix is by simply doing "man kill" or simply by executing command kill -l.
3) Bash has a built-in kill routine. So you can check that by typing /bin/kill –version