In our previous article, we explained how to find Process ID (PID or PPID) in Linux.
Today we are going to discuss a related topic that will help you to kill or terminate a hung or unresponsive program.
Basically why would you want to kill a process?
When you finish the job or close the application, the running process or program or instance will close automatically without any problems.
In some cases, if the process hangs or does not respond, you have to kill the process manually because there is no other way to close it.
To do so, you need to get the process ID of the running program.
This can be obtained using the three methods below.
- kill Command
- pkill Command
- killall Command
The following is a general syntax for process killing.
[command] [signal] [PID]
Details: -------- Command: It could be kill or pkill or killall Signal: SIGKILL (9) or SIGTERM (15) PID: Name of the process
Before testing this, we first need to understand the signals, which will help us to use the correct signal with the kill command.
The kill command provides 64 types of signals, but we are only going to discuss two signals out of 64 SIGKILL & SIGTERM
, which allow the user to send the specified signal to the specified processes or process groups.
Run the following command to see the type of signals.
# 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
SIGKILL (9):
SIGKILL refers to the Kill signal, which immediately kills or terminate the process, and this signal cannot be captured or ignored, thus it is the signal of the last attempt.
SIGTERM (15):
SIGTERM refers to terminating a signal, which is a great way to stop the process, but it can also be captured and interpreted or ignored by the process.
To kill a process, you need to find the PID of a process. Run one of the below commands to get the process ID (PID) of a process. See the article above to learn more about it.
# pidof apache2 or # pgrep apache2 or # pstree -p | grep "apache2" or # ps aux | grep "apache2"
Method-1: How to kill or Terminate a Process on Linux Using the kill Command
The kill command allows users to stop a process. The kill command sends a specific signal to a specific process or process group. If no signal is specified, the TERM signal is sent and the TERM signal destroys processes that do not capture this signal.
To prove this, we are going to find the apache2
process PID using the pstree command.
# pstree -p | grep apache2 | head -5 |-apache2(29554)-+-apache2(29555) | |-apache2(29556) | |-apache2(29557) | |-apache2(29558) | `-apache2(29559)
The above output shows the Apache2 process is running with the 29533
PID, and we’re going to stop the process using the kill command.
Run one of the commands below to terminate the associated process ID using the kill command.
# kill --verbose 29554 or # kill -9 29554 Sending signal 15 to pid 29554
Alternatively, you can do this using the process name instead of the process ID.
# kill --verbose apache2 or # kill -9 apache2 Sending signal 15 to pid 19669
Once you execute the kill command, you can check whether the process has stopped or not by running the pstree command again.
# pstree -p | grep apache2
Yes, this was successfully terminated because we don’t see any process ID for the Apache2 process.
Method-2: How to kill or Terminate a Process on Linux Using the pkill Command
pkill stands for process kill, which is a command line utility that allow users to kill or terminate the process on Linux by PID or process name.
Make a note and pkill will kill all processes that match the process name. This allows for extended regular expression patterns and other applicable criteria.
To prove this, we are going to find the MySql
process PID using the pstree command.
# pstree -p | grep mysql | head -5 |-mysqld(3861)-+-{mysqld}(3862) | |-{mysqld}(3863) | |-{mysqld}(3864) | |-{mysqld}(3865) | |-{mysqld}(3866)
The above output shows the MySql process is running with the 3861
PID, and we’re going to terminate the process using the pkill command.
Run the below command to kill the associated process ID using the pkill command.
# pkill -e mysql mysqld killed (pid 3861)
Details:
-e, --echo:
It displays what is killed
Once you execute the pkill command, you can check whether the process has stopped or not by running the pstree command again.
# pstree -p | grep mysql
Yes, this was successfully terminated because we don’t see any process ID for the MySql process.
You can also kill the specified user’s process using the Pkill command. The output below shows a list of Apache 2 processes, including the Parent Process ID (PPID) and the Child Process ID. To test this we are going to kill the root
user Apache2
process ID. See the output below.
# ps aux | grep apache2 root 822 1.5 1.3 302680 27452 ? Ss 15:39 0:00 /usr/sbin/apache2 -k start www-data 823 0.0 0.4 302704 8156 ? S 15:39 0:00 /usr/sbin/apache2 -k start www-data 824 0.0 0.4 302704 8156 ? S 15:39 0:00 /usr/sbin/apache2 -k start www-data 825 0.0 0.4 302704 8156 ? S 15:39 0:00 /usr/sbin/apache2 -k start www-data 826 0.0 0.4 302704 8156 ? S 15:39 0:00 /usr/sbin/apache2 -k start www-data 827 0.0 0.4 302704 8156 ? S 15:39 0:00 /usr/sbin/apache2 -k start root 829 0.0 0.0 12784 1020 pts/0 S+ 15:39 0:00 grep apache2
In the above output, one of the Apache2 processes belongs to the root
user, and the rest is bound to the www-data
user. We’re only going to kill the root
user Apache2 process.
# pkill -e -u root apache2 apache2 killed (pid 822)
Yes, it only killed the root user process.
Method-3: How to kill or Terminate a Process on Linux Using the killall Command
The Killall command allows users to easily stop a process using a process name instead of a PID. This is a good alternative to the kill command because you don’t need to find the PID.
To prove this, we are going to find the fail2ban
process PID using the pstree command.
# pstree -p | grep fail2ban |-fail2ban-server(19099)-+-{fail2ban-server}(19100) | |-{fail2ban-server}(19101) | |-{fail2ban-server}(19102) | `-{fail2ban-server}(19104)
The above output shows the fail2ban process is running with the 19099
PID, and we’re going to terminate the process using the killall command.
Run the below command to kill the associated process ID using the killall command.
# killall -v fail2ban-server Killed fail2ban-server(19099) with signal 15
Details:
-v, --verbose:
It reports if the signal was successfully sent.
Once you execute the killall command, you can check whether the process has stopped or not by running the pstree command again.
# pstree -p | grep fail2ban
Yes, this was successfully terminated because we don’t see any process ID for the fail2ban process.
You can also kill the specified user’s process using the killall command.
# killall -v -u root fail2ban-server
Alternatively, you can kill the specified group process using the killall command.
# killall -e -g root apache2
Method-4: How to kill or Terminate Multiple Processes on Linux Using Bash Script
If you want to find and kill multiple processes at the same time on Linux, use the following small bash script.
Use the shell script below to get the parent process ID of the given processes. To prove this, we have added httpd, crond and firewalld process and you can replace them with your process name.
# vi /opt/script/print-process-id.sh for pro in httpd crond firewalld do pgrep $pro | head -1 done
After you run the above command, you will get the result with the corresponding PPID of the given process.
# sh /opt/script/print-process-id.sh 9281 639 3361
This script is similar to the one above, but it can also kill a given process. Use the below shell script to kill multiple processes simultaneously on Linux.
# vi /opt/script/kill-process-id.sh for pro in httpd crond firewalld; do pgrep $pro; done do pro1=`pgrep $pro | head -1` echo $pro1 kill -9 $pro1 done
Finally run the Bash script to kill them.
# sh /opt/script/kill-process-id.sh
You can also use the lsof command as is documented here – https://fjolt.com/article/linux-how-to-kill-process-at-port
Hi Johnny,
Yes, we can use the lsof command, and we have included that link at the beginning of the article.