Monday, July 26, 2021

5 Example of kill command in UNIX and Linux

Kill command in UNIX and Linux is normally used to kill a suspended or hanging process or process group. Though kill is mainly associated with kill operation its merely a signal transporter and can send specified signals to specified processes 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 the task manager find the process and kill it, similarly, in UNIX and Linux we first find the process ID (PID) of the offending process and then kill it.

Though we have the killAll command also which doesn't require PID instead it can kill the process with just process name. Kill commands are 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.

Kill command examples in UNIX and Linux

As I said earlier kill sends signals to a 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 the 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 the same time and all process will be signaled or if the 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 the name of Signal if you rung it with the option "-l". For example "9" is the 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


If you want to learn more about this command and more of such essential Linux command then I highly recommend you check out the Learn Linux in 5 Days and Level Up Your Career course on Udemy. It's a great course for all kinds of people who use Linux including developers and sysadmins.

best course to learn Linux for beginners




Important points 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, the 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



That’s all on the UNIX kill command, I will add few more points as and when I recall them. You can also provide some examples of kill commands in UNIX which you may think are worth sharing and I will include them for everyone’s benefit. I can’t say happy killing in UNIX J


Some more UNIX Command tutorials

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.


6 comments:

  1. @javin
    for linux i think we can use some program like Task manager of windows?10 Challenging Java Programming Questions

    ReplyDelete
  2. What is difference between kill -6 and kill -9 option ? I knew kill -3 but never used it , which signal does it send is it SIGINT or something else? By the way is there any other command which can be used as an alternative of kill ?

    ReplyDelete
  3. @Prabhu, kill -9 is used to forcefully end a process in Unix. once you send kill -9 signal to process than it must have to shutdown.

    ReplyDelete
  4. how can i kill a zombie process,i tried to kill it using kill -9 pid ,but its not working

    ReplyDelete
  5. Hello ashwani, kill -9 will kill any process, whether it's zombie or not. Try to find the correct PID and check again.

    ReplyDelete
  6. kill -9 will not kill zombie processes, because they have already ended. They are merely a non-processed entry in the process table.

    ReplyDelete