{"id":12441,"date":"2019-03-27T01:14:25","date_gmt":"2019-03-27T01:14:25","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12441"},"modified":"2019-03-27T01:14:25","modified_gmt":"2019-03-27T01:14:25","slug":"a-guide-to-kill-pkill-and-killall-commands-to-terminate-a-process-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/27\/a-guide-to-kill-pkill-and-killall-commands-to-terminate-a-process-in-linux\/","title":{"rendered":"A Guide to Kill, Pkill and Killall Commands to Terminate a Process in Linux"},"content":{"rendered":"<p>Linux Operating System comes with Kill command to terminate a process. The command makes it possible to continue running the server without the need of reboot after a major change\/update. Here comes the great power of Linux and this is one of the reasons, why Linux is running on\u00a0<strong>90%<\/strong>\u00a0of servers, on the planet.<\/p>\n<div id=\"attachment_4184\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/a-guide-to-kill-pkill-and-killall-commands-to-terminate-a-process-in-linux\/kill-commands\/\" rel=\"attachment wp-att-4184\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4184\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/09\/Kill-Commands.png\" alt=\"Linux Kill Command\" width=\"435\" height=\"321\" aria-describedby=\"caption-attachment-4184\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-4184\" class=\"wp-caption-text\">Kill, Pkill and Killall Commands Examples<\/p>\n<\/div>\n<p><strong>Kill<\/strong>\u00a0command send a signal, a specified signal to be more perfect to a process. The kill command can be executed in a number of ways, directly or from a shell script.<\/p>\n<p>Using\u00a0<strong>kill<\/strong>\u00a0command from\u00a0<strong>\/usr\/bin<\/strong>\u00a0provide you some extra feature to kill a process by process name using\u00a0<strong>pkill<\/strong>. The common syntax for kill command is:<\/p>\n<pre># kill [signal or option] PID(s)<\/pre>\n<p>For a kill command a Signal Name could be:<\/p>\n<pre><strong>Signal Name<\/strong>\t\t<strong>Signal Value<\/strong>\t\t\t<strong>Behaviour<\/strong>\r\n\r\nSIGHUP\t\t\t      1\t\t\t\tHangup\r\nSIGKILL\t\t\t      9\t\t\t\tKill Signal\r\nSIGTERM\t\t\t      15\t\t\tTerminate<\/pre>\n<p>Clearly from the behaviour above\u00a0<strong>SIGTERM<\/strong>\u00a0is the default and safest way to kill a process.\u00a0<strong>SIGHUP<\/strong>\u00a0is less secure way of killing a process as\u00a0<strong>SIGTERM<\/strong>.\u00a0<strong>SIGKILL<\/strong>\u00a0is the most unsafe way among the above three, to kill a process which terminates a process without saving.<\/p>\n<p>In order to kill a process, we need to know the\u00a0<strong>Process ID<\/strong>\u00a0of a process. A\u00a0<strong>Process<\/strong>\u00a0is an instance of a program. Every-time a program starts, automatically an unique\u00a0<strong>PID<\/strong>\u00a0is generated for that process. Every Process in\u00a0<strong>Linux<\/strong>, have a\u00a0<strong>pid<\/strong>. The first process that starts when Linux System is booted is \u2013\u00a0<strong>init process<\/strong>, hence it is assigned a value of \u2018<strong>1<\/strong>\u2018 in most of the cases.<\/p>\n<p><strong>Init<\/strong>\u00a0is the master process and can not be killed this way, which insures that the master process don\u2019t gets killed accidentally.\u00a0<strong>Init<\/strong>\u00a0decides and allows itself to be killed, where kill is merely a request for a shutdown.<\/p>\n<p>To know all the processes and correspondingly their assigned\u00a0<strong>pid<\/strong>, run.<\/p>\n<pre># ps -A<\/pre>\n<h5>Sample Output<\/h5>\n<pre>PID TTY          TIME CMD\r\n    1 ?        00:00:01 init\r\n    2 ?        00:00:00 kthreadd\r\n    3 ?        00:00:00 migration\/0\r\n    4 ?        00:00:00 ksoftirqd\/0\r\n    5 ?        00:00:00 migration\/0\r\n    6 ?        00:00:00 watchdog\/0\r\n    7 ?        00:00:01 events\/0\r\n    8 ?        00:00:00 cgroup\r\n    9 ?        00:00:00 khelper\r\n   10 ?        00:00:00 netns\r\n   11 ?        00:00:00 async\/mgr\r\n   12 ?        00:00:00 pm\r\n   13 ?        00:00:00 sync_supers\r\n   14 ?        00:00:00 bdi-default\r\n   15 ?        00:00:00 kintegrityd\/0\r\n   16 ?        00:00:00 kblockd\/0\r\n   17 ?        00:00:00 kacpid\r\n   18 ?        00:00:00 kacpi_notify\r\n   19 ?        00:00:00 kacpi_hotplug\r\n   20 ?        00:00:00 ata\/0\r\n   21 ?        00:00:00 ata_aux\r\n   22 ?        00:00:00 ksuspend_usbd<\/pre>\n<p>How about Customising the above output using syntax as \u2018<strong>pidof process<\/strong>\u2018.<\/p>\n<pre># pidof mysqld<\/pre>\n<h5>Sample Output<\/h5>\n<pre>1684<\/pre>\n<p>Another way to achieve the above goal is to follow the below syntax.<\/p>\n<pre># ps aux | grep mysqld<\/pre>\n<h5>Sample Output<\/h5>\n<pre>root      1582  0.0  0.0   5116  1408 ?        S    09:49   0:00 \/bin\/sh \/usr\/bin\/mysqld_safe --datadir=\/var\/lib\/mysql --socket=\/var\/lib\/mysql\/mysql.sock --pid-file=\/var\/run\/mysqld\/mysqld.pid --basedir=\/usr --user=mysql\r\nmysql     1684  0.1  0.5 136884 21844 ?        Sl   09:49   1:09 \/usr\/libexec\/mysqld --basedir=\/usr --datadir=\/var\/lib\/mysql --user=mysql --log-error=\/var\/log\/mysqld.log --pid-file=\/var\/run\/mysqld\/mysqld.pid --socket=\/var\/lib\/mysql\/mysql.sock\r\nroot     20844  0.0  0.0   4356   740 pts\/0    S+   21:39   0:00 grep mysqld<\/pre>\n<p>Before we step ahead and execute a kill command, some important points to be noted:<\/p>\n<ol>\n<li>A user can kill all his process.<\/li>\n<li>A user can not kill another user\u2019s process.<\/li>\n<li>A user can not kill processes System is using.<\/li>\n<li>A root user can kill System-level-process and the process of any user.<\/li>\n<\/ol>\n<p>Another way to perform the same function is to execute \u2018<strong>pgrep<\/strong>\u2018 command.<\/p>\n<pre># pgrep mysq<\/pre>\n<h5>Sample Output<\/h5>\n<pre>3139<\/pre>\n<p>To kill the above process\u00a0<strong>PID<\/strong>, use the kill command as shown.<\/p>\n<pre>kill -9 3139<\/pre>\n<p>The above command will kill the process having\u00a0<strong>pid<\/strong>=<strong>3139<\/strong>, where\u00a0<strong>PID<\/strong>\u00a0is a\u00a0<strong>Numerical Value<\/strong>\u00a0of process.<\/p>\n<p>Another way to perform the same function, can be rewritten as.<\/p>\n<pre># kill -SIGTERM 3139<\/pre>\n<p>Similarly \u2018<strong>kill -9 PID<\/strong>\u2018 is similar to \u2018<strong>kill -SIGKILL PID<\/strong>\u2018 and vice-versa.<\/p>\n<h5>How about killing a process using process name<\/h5>\n<p>You must be aware of process name, before killing and entering a wrong process name may screw you.<\/p>\n<pre># pkill mysqld<\/pre>\n<p>Kill more than one process at a time.<\/p>\n<pre># kill PID1 PID2 PID3\r\n\r\nor\r\n\r\n# kill -9 PID1 PID2 PID3\r\n\r\nor\r\n\r\n# kill -SIGKILL PID1 PID2 PID3<\/pre>\n<p>What if a process have too many instances and a number of child processes, we have a command \u2018<strong>killall<\/strong>\u2018. This is the only command of this family, which takes process name as argument in-place of process number.<\/p>\n<h5>Syntax:<\/h5>\n<pre># killall [signal or option] Process Name<\/pre>\n<p>To kill all\u00a0<strong>mysql instances<\/strong>\u00a0along with child processes, use the command as follow.<\/p>\n<pre># killall mysqld<\/pre>\n<p>You can always verify the status of the process if it is running or not, using any of the below command.<\/p>\n<pre># service mysql status\r\n# pgrep mysql\r\n# ps -aux | grep mysql<\/pre>\n<p>That\u2019s all for now, from my side.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/how-to-kill-a-process-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux Operating System comes with Kill command to terminate a process. The command makes it possible to continue running the server without the need of reboot after a major change\/update. Here comes the great power of Linux and this is one of the reasons, why Linux is running on\u00a090%\u00a0of servers, on the planet. Kill, Pkill &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/27\/a-guide-to-kill-pkill-and-killall-commands-to-terminate-a-process-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A Guide to Kill, Pkill and Killall Commands to Terminate a Process in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12441","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12441"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12441\/revisions"}],"predecessor-version":[{"id":12442,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12441\/revisions\/12442"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}