https://www.2daygeek.com/kill-terminate-a-process-in-linux-using-kill-pkill-killall-command
We have explained in our previous article, how to find out Process ID (PID or PPID) in Linux. Today we are going to discuss about related article that will help you to kill or terminate a hung or unresponsive program.
Basically why you want to kill a process? Most of the time the running process or program or instance will be closed without any issues while you finish the job or close the application.
In some causes if the process become hung or unresponsive then you need to kill the process manually because there is no other way to close it.
This can be done using the below three methods.
Kill command offers 64 variety of signals but we are going to discuss about only two signals
SIGKILL (9) : SIGKILL stands for Kill Signal, this will kill or terminate the process immediately, this signal cannot be caught or ignored, and thus this is a signal of last resort.
SIGTERM (15) : SIGTERM stands for termination signal, this is the best way to terminate the process but it can be caught and interpreted or ignored by the process.
To kill a process, you need to find the PID of a process. Run the any one of the below commands to get Process ID (PID) of a process.
To demonstrate this, we are going to find
To demonstrate this, we are going to check whether the desicred process
Also you can kill any user’s process using pkill command. The below output shows the list of apache2 processes, which includes parent process ID (PPID) and it’s child process ID. To test this we are going to kill
To demonstrate this, we are going to check whether the desicred process
To kill particular user process.
We have explained in our previous article, how to find out Process ID (PID or PPID) in Linux. Today we are going to discuss about related article that will help you to kill or terminate a hung or unresponsive program.
Basically why you want to kill a process? Most of the time the running process or program or instance will be closed without any issues while you finish the job or close the application.
In some causes if the process become hung or unresponsive then you need to kill the process manually because there is no other way to close it.
This can be done using the below three methods.
- kill Command
- pkill Command
- killall Command
[command] [signal] [PID]Details :
- Command : It could be kill or pkill or killall
- Signal : SIGKILL (9) or SIGTERM (15)
- PID : Name of the process
SIGKILL & SIGTERM
out of 64, which allows user to send a terminate signal to process.SIGKILL (9) : SIGKILL stands for Kill Signal, this will kill or terminate the process immediately, this signal cannot be caught or ignored, and thus this is a signal of last resort.
SIGTERM (15) : SIGTERM stands for termination signal, this is the best way to terminate the process but it can be caught and interpreted or ignored by the process.
To kill a process, you need to find the PID of a process. Run the any one of the below commands to get Process ID (PID) of a process.
# pidof apache2 or # pgrep apache2 or # pstree -p | grep "apache2" or # ps aux | grep "apache2"Navigate to the following link to know more about each command.
Suggested Read : 4 Easiest Ways To Find Out Process ID (PID) In Linux
Method-1 : Using kill Command
kill allow users to terminate a process. 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.To demonstrate this, we are going to find
apache2
process PID using pstree command.# pstree -p | grep apache2 |-apache2(29554)-+-apache2(29555) | |-apache2(29556) | |-apache2(29557) | |-apache2(29558) | `-apache2(29559)The above output shows that the apache2 process is running in
29533
PID and we are going to terminate the process using kill command.# kill 29554Re-run the pstree command once again to verify whether the process is terminated or not.
# pstree -p | grep apache2Yes, it was terminated successfully because we didn’t see any process id for apache2 process.
Method-2 : Using pkill Command
pkill stands for process kill is a command line utility which allow users to kill or terminate process in Linux through PID or process name. Make a note, pkill will kill all processes matching the process name. It allows extended regular expression patterns and other matching criteria.To demonstrate this, we are going to check whether the desicred process
MySql
is running or not using pstree command.# pstree -p | grep mysql |-mysqld(3861)-+-{mysqld}(3862) | |-{mysqld}(3863) | |-{mysqld}(3864) | |-{mysqld}(3865) | |-{mysqld}(3866) | |-{mysqld}(3867) | |-{mysqld}(3868) | |-{mysqld}(3869) | |-{mysqld}(3870) | |-{mysqld}(3871) | |-{mysqld}(3872) | |-{mysqld}(3873) | |-{mysqld}(3874) | |-{mysqld}(3876) | |-{mysqld}(3877) | |-{mysqld}(3878) | |-{mysqld}(3879) | |-{mysqld}(3880) | |-{mysqld}(3881) | |-{mysqld}(3882) | |-{mysqld}(3883) | |-{mysqld}(3884) | |-{mysqld}(3885) | |-{mysqld}(3886) | |-{mysqld}(3887) | `-{mysqld}(5064)The above output shows that the MySql process is running and we are going to terminate the process using pkill command.
# pkill -e mysql mysqld killed (pid 3861)Details :
-e, --echo :
Display what is killed
# pstree -p | grep mysqlYes, it was terminated successfully because we didn’t see any process name for MySql process.
Also you can kill any user’s process using pkill command. The below output shows the list of apache2 processes, which includes parent process ID (PPID) and it’s child process ID. To test this we are going to kill
apache2
process for user root
, see the below output.# 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 apache2I can see, one apache2 process hold with
root
user, rest are binded with www-data
user. We are going to kill root
users apache2 process alone.# pkill -e -u root apache2 apache2 killed (pid 822)Yes, its killed the root user process alone.
Method-3 : Using killall Command
killall command allows user to terminate the process easily by giving process name instead of PID. It’s a good alternative to the kill command because you no need to find the PID.To demonstrate this, we are going to check whether the desicred process
fail2ban
is running or not using 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 that the fail2ban process is running and we are going to terminate the process using killall command.
# killall -v fail2ban-server Killed fail2ban-server(19099) with signal 15Details :
-v, --verbose :
Report if the signal was successfully sent.
# pstree -p | grep fail2banYes, it was terminated successfully because we didn’t see any process name for fail2ban process.
To kill particular user process.
# killall -v -u root fail2ban-serverTo kill the process group.
# kill -e -g root apache2
No comments:
Post a Comment