Kill process with Kill Pkill and Killall5 in Linux

In this tutorial we will see, How to  kill process in Linux? We will see step by step uses of kill, pkill and killall5 commands in Linux with example. So that you can easily understand how to use these commands to manage your linux kill process.

Kill process is a Linux inbuilt commands which is use to manage Linux process. Kill is a Linux commands which is used to send a signal to the process. Signal can be SIGHUP (1), SIGKILL (9) and SIGTERM (15). By default kill command send termination signal to the process.

There are some rules of kill commands  in Linux:-

  1. User can kill all his process.
  2. User can not kill another user’s process.
  3. User can not kill processes System is using.
  4. Root user can kill System-level-process and the process of any user.

How to use kill command

First off all we have to identify which process we want to kill or terminate. We can identify our running process with the help of PS and TOP commands. You can see below I am going to list down NTP service using ps commands.

[[email protected] ~]# ps -ef | grep ntp
ntp       6307     1  0 Aug08 ?        00:00:01 /usr/sbin/ntpd -u ntp:ntp -g
root      9467  6248  0 09:59 pts/0    00:00:00 grep --color=auto ntp
[[email protected] ~]#
[[email protected] ~]# systemctl status ntpd
ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2018-08-08 09:33:02 CEST; 5 days ago
  Process: 6306 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 6307 (ntpd)
   CGroup: /system.slice/ntpd.service
           └─6307 /usr/sbin/ntpd -u ntp:ntp –g

You can see our NTP server is running, and our ntp service process ID is 6307. If we want to kill our ntp service then we have run this commands kill 6307 you can see in below example.

#Command:-kill <Process ID>
[[email protected] ~]# kill 6307
[[email protected] ~]#

We can also use signal 9 to kill this process and signal 15 to terminate this process. Like below.

[[email protected] ~]# ps -ef|grep ntp
ntp      11809     1  0 11:47 ?        00:00:00 /usr/sbin/ntpd -u ntp:ntp -g
root     11818 11753  0 11:47 pts/0    00:00:00 grep --color=auto ntp
[[email protected] ~]#
[[email protected] ~]# kill -9 11809
[[email protected] ~]# ps -ef|grep ntp
root     11822 11753  0 11:48 pts/0    00:00:00 grep --color=auto ntp
[[email protected] ~]#
[[email protected] ~]# ps -ef|grep ntp
ntp      11840     1  0 11:48 ?        00:00:00 /usr/sbin/ntpd -u ntp:ntp -g
root     11842 11753  0 11:48 pts/0    00:00:00 grep --color=auto ntp
[[email protected] ~]#
[[email protected] ~]# kill -15 11840
[[email protected] ~]# ps -ef|grep ntp
root     11846 11753  0 11:49 pts/0    00:00:00 grep --color=auto ntp
[[email protected] ~]#

You can see in above example we have successfully kill process of our ntp service. We can verify our ntp service is running or not like below.

[[email protected] ~]# systemctl status ntpd
ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Thu 2018-08-16 11:49:24 CEST; 12min ago
  Process: 11839 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 11840 (code=exited, status=0/SUCCESS)

You can see in above output our NTP server is inactive. So we can use kill command to stop any process.

We can also kill one or more process at the same time using kill command like below.

[[email protected] ~]# ps -ef|grep http
root     11781     1  0 11:43 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11782 11781  0 11:43 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11783 11781  0 11:43 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11784 11781  0 11:43 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11785 11781  0 11:43 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11786 11781  0 11:43 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root     11884 11753  0 12:03 pts/0    00:00:00 grep --color=auto http
[[email protected]  ~]# kill -9 11781 11782 11783 11784 11785 11786
[[email protected] ~]# ps -ef|grep http
root     11891 11753  0 12:04 pts/0    00:00:00 grep --color=auto http
[[email protected] ~]#

Now we will see What is pkill command and how we can use it?

We can use pkill command to kill process with specifying name or any matching pattern. You can see in below example. We need to kill our httpd service. You can see here we have lots of process id of httpd service. So here we can use pkill command with service name to kill all process of specified name.

[[email protected] ~]# ps -ef |grep httpd
root     11497     1  0 08:09 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11498 11497  0 08:09 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11499 11497  0 08:09 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11500 11497  0 08:09 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11501 11497  0 08:09 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11502 11497  0 08:09 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root     11520 11337  0 08:29 pts/0    00:00:00 grep --color=auto httpd
[[email protected] ~]#

You can see in below our httpd service is running, and you can also check process id of httpd service in above output.

[[email protected] ~]# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-08-16 08:09:18 CEST; 28min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 11497 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─11497 /usr/sbin/httpd -DFOREGROUND
           ├─11498 /usr/sbin/httpd -DFOREGROUND
           ├─11499 /usr/sbin/httpd -DFOREGROUND
           ├─11500 /usr/sbin/httpd -DFOREGROUND
           ├─11501 /usr/sbin/httpd -DFOREGROUND
           └─11502 /usr/sbin/httpd -DFOREGROUND


Aug 16 08:09:18 tzclouds systemd[1]: Starting The Apache HTTP Server...
Aug 16 08:09:18 tzclouds httpd[11497]: AH00558: httpd: Could not reliably determine the server's fully q...ssage
Aug 16 08:09:18 tzclouds systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[[email protected] ~]#

Now we are going to use pkill command with service name to kill httpd service.

[[email protected] ~]# pkill httpd
[[email protected] ~]#

You can see in above output  we have successfully kill our httpd service with name. Now we can check our httpd service has been stop or not.

[[email protected] ~]# ps -ef |grep httpd
root     11541 11337  0 08:46 pts/0    00:00:00 grep --color=auto httpd
[[email protected] ~]#

In above output you can see all process of httpd has been killed. You can also check with systemctl status httpd commands to check our httpd service running or not like below.

[[email protected] ~]# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2018-08-16 08:45:17 CEST; 3min 34s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 11532 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
  Process: 11497 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
 Main PID: 11497 (code=exited, status=0/SUCCESS)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
Aug 16 08:09:18 tzclouds systemd[1]: Starting The Apache HTTP Server...
Aug 16 08:09:18 tzclouds httpd[11497]: AH00558: httpd: Could not reliably determine the server's fully q...ssage
Aug 16 08:09:18 tzclouds systemd[1]: Started The Apache HTTP Server.
Aug 16 08:45:17 tzclouds kill[11532]: kill: cannot find process ""
Aug 16 08:45:17 tzclouds systemd[1]: httpd.service: control process exited, code=exited status=1
Aug 16 08:45:17 tzclouds systemd[1]: Unit httpd.service entered failed state.
Aug 16 08:45:17 tzclouds systemd[1]: httpd.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
[[email protected] ~]#

In above output you can see our httpd service has been terminated and stop.

We can also start our httpd service using systemctl start httpd and check our httpd service is up or not? Now we need to be verify httpd service status using systemctl status httpd or ps –ef |grep httpd like below.

[[email protected] ~]# systemctl start httpd
[[email protected] ~]# systemctl status httpd

httpd.service - The Apache HTTP Server

   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-08-16 08:51:06 CEST; 6s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 11532 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
 Main PID: 11550 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─11550 /usr/sbin/httpd -DFOREGROUND
           ├─11551 /usr/sbin/httpd -DFOREGROUND
           ├─11552 /usr/sbin/httpd -DFOREGROUND
           ├─11553 /usr/sbin/httpd -DFOREGROUND
           ├─11554 /usr/sbin/httpd -DFOREGROUND
           └─11555 /usr/sbin/httpd -DFOREGROUND

Aug 16 08:51:06 tzclouds systemd[1]: Starting The Apache HTTP Server...
Aug 16 08:51:06 tzclouds httpd[11550]: AH00558: httpd: Could not reliably determine the server's fully q...ssage
Aug 16 08:51:06 tzclouds systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

[[email protected] ~]# ps -ef |grep httpd
root     11550     1  0 08:51 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11551 11550  0 08:51 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11552 11550  0 08:51 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11553 11550  0 08:51 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11554 11550  0 08:51 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11555 11550  0 08:51 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root     11559 11337  0 08:51 pts/0    00:00:00 grep --color=auto httpd
[[email protected]~]#

You can see in above output our httpd service is up and running.

What is killall5 Linux command and how can we use it?

Killall5 is the System killall command. It sends a signal to all processes except kernel threads and the processes in its own session, so killall5 won’t kill the shell that is running the script it was called from.

If you want to kill all process of your current session you can run killall5, then you can see all process has been killed. Now we can see with example in below.

You can see currently these services are running.

[[email protected]~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      950/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1054/master
tcp6       0      0 :::80                   :::*                    LISTEN      11550/httpd
tcp6       0      0 :::22                   :::*                    LISTEN      950/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1054/master
[[email protected] ~]#

Now run killall5 command and see.

[[email protected] ~]# killall5
login as: root
[email protected]'s password:
Last login: Thu Aug 16 08:04:15 2018 from 10.160.0.134
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      11748/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      11748/sshd
[[email protected]  ~]#

You can see in above example we have killed all services after run killall5 command. Our ssh session has disconnected and after reconnect we can see only sshd service is running apart from this all services are down.

Thant’s all, In this tutorial we have seen kill, pkill and killall5 Linux commands uses with example.

Linux Firewall manage in RHEL 7 and CentOS 7

 

6 thoughts on “Kill process with Kill Pkill and Killall5 in Linux

  1. It is truly a great and useful piece of information. I am satisfied that you just shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

  2. Is it ok to write more about this? You’ve written a highly informative post nonetheless, so bless you!|

Leave a Reply

Your email address will not be published. Required fields are marked *