Cyberithub

10 Popular Kill Command Examples in Linux/Unix(How to Kill a Process in Linux)

In this tutorial, i will take you through 10 Popular Kill Command Examples in Linux/Unix. kill command is a very popular command in Linux/Unix based Systems to terminate a process. kill command relies on list of signals to terminate any of the System processes(User Space Processes). By default kill command sends the TERM signal to kill the process. A process can usually be killed by either process name or by its PID(Process ID). There are other tools like killall, pkill, xkill which can be used along with kill command to terminate the processes but as of now we will keep our focus on kill command only in this tutorial. We will cover other tools in upcoming tutorials.

SYNOPSIS

kill [-s signal|-p] [-q sigval] [-a] [–] pid…
kill -l [signal]

10 Popular Kill Command Examples in Linux/Unix(How to Kill a Process in Linux) 1

Kill Command Examples in Linux/Unix(How to Kill a Process in Linux)

Also Read: 7 Easy Steps to Install PHP on RHEL 8/CentOS 8

Example 1: How to Print a List of Signal Names(On RHEL/CentOS Servers)

If you want to print a list of Signal Names available for use with kill command then you need to use kill -l command as shown below.

[root@localhost ~]# 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

-l : Print a list of signal names, or convert signal given as argument to a name. More on kill command Man Page.

NOTE:

Please note that here I am using root user to run all the below commands. You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

Example 2: How to List all the available Signal Choices in a Nice Table(On Ubuntu Servers)

If you want to show the List of Signals available for use with kill command on Ubuntu Based Servers then you need to use kill -L command as shown below.

root@localhost:~# 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

-L : List signal names in a nice table.

Example 3: How to Translate Signal Number into Signal Name

If you want to translate Signal number to Signal name then you need to use -l option with Kill command as shown below. As you can see from below output, 12 represents signal USR2, 20 represents signal TSTP and 38 represents signal RTMIN+4.

root@localhost:~# kill -l 12
USR2
root@localhost:~# kill -l 20
TSTP
root@localhost:~# kill -l 38
RTMIN+4

Example 4: How to Kill All the Processes that can be Killed

If you want to Kill all the processes in a Server that can be killed then you need to use kill -9 -1 command as shown below.

root@localhost:~# kill -9 -1

NOTE:

Please be very careful while running above command in a Production Server as these might become disastrous. This command might close your session and log you out from the Prod Server. Also this command can close any important running processes.

Example 5: How to Kill a Process by PID using kill command

If you want to simply kill a process by its PID then you need to use kill <PID> command as shown below. In this example, we are trying to terminal process of PID value 1268 using kill 1268 command.

[root@localhost ~]# kill 1268

Example 6: How to Kill a Process in Linux Using SIGKILL

If you want to kill a process by using SIGKILL signal then you need to use below kill command. Here we are trying to first identifying the process by using ps command which we want to kill. It is the sendmail process which is currently running on the server.

[root@localhost ~]# ps -ef | grep -i 3330 | grep -v grep
root 3330 1 0 01:23 ? 00:00:00 sendmail: accepting connections

Then we are terminating sendmail process by using either signal number or signal name with kill command as shown below.

[root@localhost ~]# kill -9 3330
[root@localhost ~]# kill -SIGKILL 3330

Example 7: How to Forcefully Kill a Process using kill command

If you want to forcefully kill a process in Linux then you need to use -9 signal number with kill command as shown below. Firstly we are trying to check the process by using ps command which we want to terminate.

[root@localhost ~]# ps -ef | grep -i 3330 | grep -v grep
root 3330 1 0 01:23 ? 00:00:00 sendmail: accepting connections

Now to kill the sendmail process forcefully whose PID value is 3330 we need to use kill -9 3330 command as shown below.

[root@localhost ~]# kill -9 3330

Example 8: How to Kill multiple processes in Linux Using Kill command

If you want to Kill multiple processes in a single command then you can first get the PID of all those processes as shown below. It is always a good idea to double check the PID value of a process before terminating it.

[root@localhost ~]# ps -ef | grep -i 3730 | grep -v grep
root 3730 1 0 02:03 ? 00:00:00 sendmail: accepting connections
[root@localhost ~]# ps -ef | grep -i 3745 | grep -v grep
smmsp 3745 1 0 02:03 ? 00:00:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue

Then provide those PID values with below Kill command to terminate all those processes as shown below.

[root@localhost ~]# kill 3330 3345

Example 9: How to Use ps command with kill command to terminate a Process in Linux

You can also use ps command with kill command to terminate a process through its PID as shown below. In this example, we are trying to kill below sendmail process.

[root@localhost ~]# ps -ef | grep -i sendmail | grep -v grep
root 3730 1 0 02:03 ? 00:00:00 sendmail: accepting connections
smmsp 3745 1 0 02:03 ? 00:00:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue

We are also using sed and awk tool with ps command to get the process ID of sendmail process that we want to kill as mentioned below.

[root@localhost ~]# kill -9 $(ps -ef | grep -i sendmail | grep -v grep | sed -n 1p | awk '{ print $2 }')

You can again verify the process if it is killed or not from below ps command.

[root@localhost ~]# ps -ef | grep -i sendmail | grep -v grep

Example 10: How to Check Man Page of kill Command in Linux

If you want to check man page of kill command in Linux then you need to use man kill command as shown below.

[root@localhost ~]# man kill
KILL(1) User Commands KILL(1)

NAME
kill - terminate a process

SYNOPSIS
kill [-s signal|-p] [-q sigval] [-a] [--] pid...
kill -l [signal]

DESCRIPTION
The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM signal is sent. The TERM signal
will kill processes which do not catch this signal. For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be
caught.

Most modern shells have a builtin kill function, with a usage rather similar to that of the command described here. The '-a' and '-p' options, and the pos‐
sibility to specify processes by command name are a local extension.

If sig is 0, then no signal is sent, but error checking is still performed.

OPTIONS
pid... Specify the list of processes that kill should signal. Each pid can be one of five things:

n where n is larger than 0. The process with pid n will be signaled.

0 All processes in the current process group are signaled.

-1 All processes with pid larger than 1 will be signaled.


 

Popular Recommendations:-

Easy Steps to Install Java on Ubuntu 20.04

Best Explanation of Wrapper Classes in Java: Autoboxing and Unboxing with Examples

Best Steps to Install Java on RHEL 8/CentOS 8

15 ansible-vault command examples to encrypt and decrypt sensitive data/files on Linux

Learn HTML Image Maps(v5) with Best Examples

C# data types with Best Examples (.NET v4.7)

How to Install PHP on RedHat/CentOS 7 with Easy Steps

How to Install Ruby on Ubuntu 18.04 with Easy Steps

Leave a Comment