{"id":8583,"date":"2024-11-21T03:00:00","date_gmt":"2024-11-21T08:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=8583"},"modified":"2024-11-19T15:05:53","modified_gmt":"2024-11-19T20:05:53","slug":"managing-processes-with-kill-and-killall","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=8583","title":{"rendered":"Managing processes with kill and killall"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"8583\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>In Linux, every program and daemon is a \u201cprocess.\u201d Most processes represent a single running program. Other programs can fork off other processes, such as processes to listen for certain things to happen and then respond to them. And each process requires a certain amount of memory and processing power. The more processes you have running, the more memory and CPU cycles you\u2019ll need. On older systems, like my 12-year-old laptop, or smaller computers like the Raspberry Pi, you can get the most out of your system if you keep an eye on what processes you have running in the background.<\/p>\n\n\n\n<p>You can get a list of running processes with the&nbsp;<strong>ps<\/strong>&nbsp;command. You\u2019ll usually want to give ps some options to show more information in its output. I like to use the&nbsp;<strong>-e<\/strong>&nbsp;option to see every process running on my system, and the&nbsp;<strong>-f<\/strong>&nbsp;option to get full details about each process. Here are some examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ps\n    PID TTY          TIME CMD\n  11289 pts\/1    00:00:00 bash\n  11292 pts\/1    00:00:00 ps<\/code><\/pre>\n\n\n\n<p>Or, for more detail about every (<strong>-e<\/strong>) process:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ps -e | head\n    PID TTY          TIME CMD\n      1 ?        00:00:01 systemd\n      2 ?        00:00:00 kthreadd\n      3 ?        00:00:00 pool_workqueue_release\n      4 ?        00:00:00 kworker\/R-rcu_gp\n      5 ?        00:00:00 kworker\/R-sync_wq\n      6 ?        00:00:00 kworker\/R-slub_flushwq\n      7 ?        00:00:00 kworker\/R-netns\n      9 ?        00:00:00 kworker\/0:0H-events_highpri\n     12 ?        00:00:00 kworker\/R-mm_percpu_wq<\/code><\/pre>\n\n\n\n<p>And for full (<strong>-f<\/strong>) details:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ps -ef | head\nUID          PID    PPID  C STIME TTY          TIME CMD\nroot           1       0  0 09:46 ?        00:00:01 \/usr\/lib\/systemd\/systemd --switched-root --system --deserialize=40 rhgb\nroot           2       0  0 09:46 ?        00:00:00 &#91;kthreadd]\nroot           3       2  0 09:46 ?        00:00:00 &#91;pool_workqueue_release]\nroot           4       2  0 09:46 ?        00:00:00 &#91;kworker\/R-rcu_gp]\nroot           5       2  0 09:46 ?        00:00:00 &#91;kworker\/R-sync_wq]\nroot           6       2  0 09:46 ?        00:00:00 &#91;kworker\/R-slub_flushwq]\nroot           7       2  0 09:46 ?        00:00:00 &#91;kworker\/R-netns]\nroot           9       2  0 09:46 ?        00:00:00 &#91;kworker\/0:0H-events_highpri]\nroot          12       2  0 09:46 ?        00:00:00 &#91;kworker\/R-mm_percpu_wq]<\/code><\/pre>\n\n\n\n<p>The last example shows the most detail. On each line, the UID (user ID) shows the user that owns the process. The PID (process ID) represents the numerical ID of each process, and PPID (parent process ID) shows the ID of the process that spawned this one. In any Unix system, processes count up from PID 1, the first process to run once the kernel starts up. Here,&nbsp;<strong>systemd<\/strong>&nbsp;is the first process, which spawned&nbsp;<strong>kthreadd<\/strong>. And&nbsp;<strong>kthreadd<\/strong>&nbsp;created other processes including&nbsp;<strong>pool_workqueue_release<\/strong>&nbsp;and several \u201cworker\u201d processes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"managing-processes-with-kill\">Managing processes with&nbsp;<code>kill<\/code><\/h2>\n\n\n\n<p>The system will take care of most background processes on its own, so you don\u2019t need to worry about them. You should only have to get involved in managing any processes that you create, usually by running applications. While many applications run one process at a time (think about your music player or terminal emulator or game), other applications might create background processes. Some of these might keep running when you exit the application so they can get back to work quickly the next time you start the application.<\/p>\n\n\n\n<p>Process management is an issue when I run the Chrome browser. Chrome works my laptop pretty hard and fires off a lot of extra processes. Right now, I can see these Chrome processes running with only a few tabs open:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ps -ef | grep chrome | wc -l\n24\n\n$ ps -ef | grep chrome | head\njhall       2471    1920  3 09:47 ?        00:08:03 \/opt\/google\/chrome\/chrome\njhall       2478       1  0 09:47 ?        00:00:00 \/opt\/google\/chrome\/chrome_crashpad_handler --monitor-self --monitor-self-annotation=ptype=crashpad-handler --database=\/home\/jhall\/.config\/google-chrome\/Crash Reports --url=https:\/\/clients2.google.com\/cr\/report --annotation=channel= --annotation=lsb-release=Fedora Linux 40 (Xfce) --annotation=plat=Linux --annotation=prod=Chrome_Linux --annotation=ver=131.0.6778.69 --initial-client-fd=5 --shared-client-connection\njhall       2480       1  0 09:47 ?        00:00:00 \/opt\/google\/chrome\/chrome_crashpad_handler --no-periodic-tasks --monitor-self-annotation=ptype=crashpad-handler --database=\/home\/jhall\/.config\/google-chrome\/Crash Reports --url=https:\/\/clients2.google.com\/cr\/report --annotation=channel= --annotation=lsb-release=Fedora Linux 40 (Xfce) --annotation=plat=Linux --annotation=prod=Chrome_Linux --annotation=ver=131.0.6778.69 --initial-client-fd=4 --shared-client-connection\njhall       2486    2471  0 09:47 ?        00:00:00 \/opt\/google\/chrome\/chrome --type=zygote --no-zygote-sandbox --string-annotations --crashpad-handler-pid=2478 --enable-crash-reporter=, --change-stack-guard-on-fork=enable\njhall       2487    2471  0 09:47 ?        00:00:00 \/opt\/google\/chrome\/chrome --type=zygote --string-annotations --crashpad-handler-pid=2478 --enable-crash-reporter=, --change-stack-guard-on-fork=enable\njhall       2489    2487  0 09:47 ?        00:00:00 \/opt\/google\/chrome\/chrome --type=zygote --string-annotations --crashpad-handler-pid=2478 --enable-crash-reporter=, --change-stack-guard-on-fork=enable\njhall       2514    2486  3 09:47 ?        00:07:06 \/opt\/google\/chrome\/chrome --type=gpu-process --string-annotations --crashpad-handler-pid=2478 --enable-crash-reporter=, --change-stack-guard-on-fork=enable --gpu-preferences=UAAAAAAAAAAgAAAEAAAAAAAAAAAAAAAAAABgAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAABAAAAAAAAAAEAAAAAAAAAAIAAAAAAAAAAgAAAAAAAAA --shared-files --field-trial-handle=3,i,18298794817931529311,15571106924415392790,262144 --variations-seed-version=20241118-164138.616000\njhall       2517    2471  0 09:47 ?        00:02:00 \/opt\/google\/chrome\/chrome --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=none --string-annotations --crashpad-handler-pid=2478 --enable-crash-reporter=, --change-stack-guard-on-fork=enable --shared-files=v8_context_snapshot_data:100 --field-trial-handle=3,i,18298794817931529311,15571106924415392790,262144 --variations-seed-version=20241118-164138.616000\njhall       2554    2489  0 09:47 ?        00:00:01 \/opt\/google\/chrome\/chrome --type=utility --utility-sub-type=storage.mojom.StorageService --lang=en-US --service-sandbox-type=utility --string-annotations --crashpad-handler-pid=2478 --enable-crash-reporter=, --change-stack-guard-on-fork=enable --shared-files=v8_context_snapshot_data:100 --field-trial-handle=3,i,18298794817931529311,15571106924415392790,262144 --variations-seed-version=20241118-164138.616000\njhall       2624    2489  1 09:47 ?        00:02:16 \/opt\/google\/chrome\/chrome --type=renderer --string-annotations --crashpad-handler-pid=2478 --enable-crash-reporter=, --change-stack-guard-on-fork=enable --lang=en-US --num-raster-threads=2 --enable-main-frame-before-activation --renderer-client-id=10 --time-ticks-at-unix-epoch=-1732031201381244 --launch-time-ticks=47432658 --shared-files=v8_context_snapshot_data:100 --field-trial-handle=3,i,18298794817931529311,15571106924415392790,262144 --variations-seed-version=20241118-164138.616000<\/code><\/pre>\n\n\n\n<p>By default, Chrome will also keep these processes running after you exit the application. You can see this for yourself in the Chrome setting for \u201cContinue running background apps when Google Chrome is closed\u201d (under \u201cSystem\u201d). You might turn off this feature, or Chrome processes will keep running when you close the application, and continue to use memory. This extra usage use isn\u2019t usually a problem on systems that have a fast CPU and are stuffed with memory, like my desktop PC. But my older laptop has limited memory, yet I use it all the time to do presentations. I don\u2019t want to keep these extra processes running after I exit the browser. If I forget to change this setting, my older laptop suffers even after I quit Chrome.<\/p>\n\n\n\n<p>Fortunately, you can use the command line to stop these processes after you exit Chrome. The&nbsp;<strong>kill<\/strong>&nbsp;command lets you terminate a process. In the simplest case, you tell&nbsp;<strong>kill<\/strong>&nbsp;the PID of what you want to stop. For example, to terminate each of these processes, I would need to execute the&nbsp;<strong>kill<\/strong>&nbsp;command against each of the process IDs. One way to do that is with a command line that gets the PIDs and another that runs&nbsp;<strong>kill<\/strong>&nbsp;against that list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ps -ef | grep '\/opt\/google\/chrome\/chrome' | awk '{print $2}'\n2471\n2478\n2480\n2486\n2487\n2489\n2514\n2517\n2554\n2624\n2646\n2656\n2699\n2721\n4582\n4685\n4697\n8945\n9512\n9521\n11022\n11240\n11439\n11660<\/code><\/pre>\n\n\n\n<p>And:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ps -ef | grep '\/opt\/google\/chrome\/chrome' | awk '{print $2}' &gt; pids\n$ kill $(cat pids)<\/code><\/pre>\n\n\n\n<p>The first command line generates a list of process IDs for the Chrome browser. The second command line runs the&nbsp;<strong>kill<\/strong>&nbsp;command against that list of process IDs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-killall-command\">The&nbsp;<code>killall<\/code>&nbsp;command<\/h2>\n\n\n\n<p>A simpler way to stop a bunch of processes all at once is to use the&nbsp;<strong>killall<\/strong>&nbsp;command. As you might guess by the name,&nbsp;<strong>killall<\/strong>&nbsp;terminates all processes that match a name. That means we can use this command to stop all of our rogue Chrome processes. This is as simple as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ killall \/opt\/google\/chrome\/chrome<\/code><\/pre>\n\n\n\n<p>But be careful with&nbsp;<strong>killall<\/strong>. This command can terminate any process that matches what you give it. That\u2019s why I like to first use&nbsp;<strong>ps -ef<\/strong>&nbsp;to check my running processes, then run&nbsp;<strong>killall<\/strong>&nbsp;against the exact path to the command that I want to stop.<\/p>\n\n\n\n<p>You might also want to use the\u00a0<strong>-i<\/strong>\u00a0or\u00a0<strong>&#8211;interactive<\/strong>\u00a0option to ask\u00a0<strong>killall<\/strong>\u00a0to prompt you before it stops each process.<\/p>\n\n\n\n<p><strong>killall<\/strong>\u00a0also supports options to select processes that are older than a specific time using the\u00a0<strong>-o<\/strong>\u00a0or\u00a0<strong>&#8211;older-than<\/strong>\u00a0option. This can be helpful if you discover a set of rogue processes that have been running unattended for several days, for example. Or you can select processes that are younger than a specific time, such as runaway processes you recently started. Use the\u00a0<strong>-y<\/strong>\u00a0or\u00a0<strong>&#8211;younger-than<\/strong>\u00a0option to select these processes.<\/p>\n\n\n\n<p>Process management can be an important part of system maintenance. In my early career as a Unix and Linux systems administrator, the ability to end escaped jobs was a useful tool to keep systems running properly. You may not need to stop rogue processes in a modern Linux desktop, but knowing&nbsp;<strong>kill<\/strong>&nbsp;and&nbsp;<strong>killall<\/strong>&nbsp;can help you when things eventually go awry. It\u2019s always a good idea to keep an eye on what processes are running on your system and know how to manage them when needed.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>This article is adapted from&nbsp;<a href=\"https:\/\/opensource.com\/article\/20\/1\/linux-kill-killall\">Managing processes on Linux with kill and killall<\/a>&nbsp;by Jim Hall, and is republished with the author\u2019s permission.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Process management can be an important part of system maintenance.<\/p>\n","protected":false},"author":33,"featured_media":4654,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5,89],"tags":[91,97],"class_list":["post-8583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-system-administration","tag-linux","tag-sysadmin"],"modified_by":"Jim Hall","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8583"}],"version-history":[{"count":1,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8583\/revisions"}],"predecessor-version":[{"id":8584,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8583\/revisions\/8584"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/4654"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}