{"id":3857,"date":"2015-10-23T20:48:23","date_gmt":"2015-10-23T20:48:23","guid":{"rendered":"http:\/\/really.zonky.org\/?p=3857"},"modified":"2015-10-23T20:48:23","modified_gmt":"2015-10-23T20:48:23","slug":"tinkering-with-the-ps-command","status":"publish","type":"post","link":"https:\/\/really.zonky.org\/?p=3857","title":{"rendered":"Tinkering With The PS Command"},"content":{"rendered":"<p>\n\tHave you ever wondered if you can tinker with the&nbsp;<em>ps<\/em>&nbsp;command to change how and what is displayed? No? Well give up reading this post then.\n<\/p>\n<p>\n\tI&#39;ve known about&nbsp;<em>ps<\/em>&nbsp;for ages and also the way that the output can be tinkered with, but have not had an excuse to dig into it properly until I was looking for a way for&nbsp;<em>ps<\/em>&nbsp;to show the Linux container name for each process (don&#39;t get excited:&nbsp;<em>ps -o machine<\/em>&nbsp;is documented but not implemented at the time of writing).&nbsp;\n<\/p>\n<p>\n\tIf you read the manual page for&nbsp;<em>ps<\/em>&nbsp;you will be quickly distracted by all the different options available. These can be grossly simplified into three different groups of options: which processes to list, what to output, and how to sort the output.\n<\/p>\n<h2>\n\tWhich Processes?<br \/>\n<\/h2>\n<p>\n\tThis can be simplified down to almost nothing;&nbsp;<em>ps<\/em>&nbsp;on it&#39;s own lists just the processes running from the current terminal (window) :-\n<\/p>\n<pre>\r\n% <strong>ps<\/strong>\r\n&nbsp; PID TTY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TIME CMD\r\n&nbsp;2591 pts\/17 &nbsp; 00:00:00 zsh\r\n13325 pts\/17 &nbsp; 00:00:00 ps\r\n<\/pre>\n<p>\n\tIf you want to display all processes, add the &quot;-e&quot; option :-\n<\/p>\n<pre>\r\n% <strong>ps -e<\/strong>\r\n&nbsp; PID TTY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TIME CMD\r\n&nbsp;&nbsp;&nbsp; 1 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00:00:03 systemd\r\n&nbsp;&nbsp;&nbsp; 2 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00:00:00 kthreadd\r\n&nbsp;&nbsp;&nbsp; 3 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00:00:00 ksoftirqd\/0\r\n&nbsp;&nbsp;&nbsp; 5 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00:00:00 kworker\/0:0H\r\n&nbsp;&nbsp;&nbsp; 7 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00:00:54 rcu_sched\r\n&nbsp;&nbsp;&nbsp; 8 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00:00:00 rcu_bh\r\n&nbsp;&nbsp;&nbsp; 9 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00:00:35 rcuos\/0\r\n&nbsp;&nbsp; 10 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00:00:00 rcuob\/0\r\n&nbsp;&nbsp; 11 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00:00:00 migration\/0\r\n&nbsp;&nbsp; 12 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00:00:00 watchdog\/0\r\n(<em>cut<\/em>)\r\n<\/pre>\n<p>\n\tAnd lastly (not literally &#8211; there are other options), add the &quot;-p&quot; option to list processes by process ID :-\n<\/p>\n<pre>\r\n% <strong>ps -p 1<\/strong>\r\n&nbsp; PID TTY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TIME CMD\r\n&nbsp; &nbsp; 1 ? &nbsp; &nbsp; &nbsp; &nbsp;00:00:03 systemd\r\n<\/pre>\n<h2>\n\tTuning The Output<br \/>\n<\/h2>\n<p>\n\tBy default the fields that&nbsp;<em>ps<\/em>&nbsp;outputs is somewhat peculiar until you realise that the output fields have been frozen in time. The default choice is somewhat minimal; and I&#39;m not in favour of minimalism. And what use is the TTY and the TIME fields?\n<\/p>\n<p>\n\tThe TTY field shows you what terminal the process is running on &#8211; this was handy on a multi-user system where you could find out who was on what terminal and then&nbsp;<em>write<\/em>&nbsp;a message directly to their screen. A great way of winding people up, but not so much use these days. And TIME? We&#39;re no longer billed for the cpu time we consume, so the time spent running on the cpu is a rather pointless thing to list.\n<\/p>\n<p>\n\tThe &quot;-f&quot; option displays more information :-\n<\/p>\n<pre>\r\n% <strong>ps -f<\/strong>\r\nUID &nbsp; &nbsp; &nbsp; &nbsp;PID &nbsp;PPID &nbsp;C STIME TTY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TIME CMD\r\nmike &nbsp; &nbsp; 26486 31092 &nbsp;0 21:24 pts\/24 &nbsp; 00:00:00 ps -f\r\nmike &nbsp; &nbsp; 31092 31091 &nbsp;0 20:11 pts\/24 &nbsp; 00:00:00 -zsh\r\n<\/pre>\n<p>\n\tBut the output is still somewhat peculiar, and there are other more interesting fields to display.\n<\/p>\n<p>\n\tThere&nbsp;<em>are<\/em>&nbsp;various options for choosing the output format amongst a set of predefined choices, but the best bet is to ignore these and jump straight into selecting the individual fields that you want. These can be found in the manual page in the &quot;STANDARD FORMAT SPECIFIERS&quot; section. Simply list the fields you want after the &quot;-o&quot; option :-\n<\/p>\n<pre>\r\n% <strong>ps -o pid,comm,pcpu,pmem,nlwp,user,stat,sgi_p,wchan,class,pri,nice,flags<\/strong>\r\n&nbsp; PID COMMAND &nbsp; &nbsp; &nbsp; &nbsp; %CPU %MEM NLWP USER &nbsp; &nbsp; STAT P WCHAN &nbsp;CLS PRI &nbsp;NI F\r\n28061 ps &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.0 &nbsp;0.0 &nbsp; &nbsp;1 mike &nbsp; &nbsp; R+ &nbsp; 3 - &nbsp; &nbsp; &nbsp;TS &nbsp; 19 &nbsp; 0 0\r\n31092 zsh &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.0 &nbsp;0.0 &nbsp; &nbsp;1 mike &nbsp; &nbsp; Ss &nbsp; * - &nbsp; &nbsp; &nbsp;TS &nbsp; 19 &nbsp; 0 0\r\n<\/pre>\n<p>\n\tObviously typing this in every time is somewhat less than ideal, but fortunately the authors of&nbsp;<em>ps<\/em>&nbsp;have already thought of this. By listing the fields within the PS_FORMAT environment variable, there is no need to specify -o :-\n<\/p>\n<pre>\r\n% <strong>export PS_FORMAT=&quot;pid,comm,pcpu,pmem,nlwp,user,stat,sgi_p,wchan,class,pri,nice,flags&quot;<\/strong>\r\n% <strong>ps<\/strong>\r\n&nbsp; PID COMMAND &nbsp; &nbsp; &nbsp; &nbsp; %CPU %MEM NLWP USER &nbsp; &nbsp; STAT P WCHAN &nbsp;CLS PRI &nbsp;NI F\r\n29440 ps &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.0 &nbsp;0.0 &nbsp; &nbsp;1 mike &nbsp; &nbsp; R+ &nbsp; 5 - &nbsp; &nbsp; &nbsp;TS &nbsp; 19 &nbsp; 0 0\r\n31092 zsh &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.0 &nbsp;0.0 &nbsp; &nbsp;1 mike &nbsp; &nbsp; Ss &nbsp; * - &nbsp; &nbsp; &nbsp;TS &nbsp; 19 &nbsp; 0 0\r\n<\/pre>\n<p>\n\tTo make this pernament, add this to your shell startup rc file; whilst editing you may as well set PS_PERSONALITY to &quot;linux&quot;.\n<\/p>\n<h2>\n\tSorting The Output<br \/>\n<\/h2>\n<p>\n\tAccording to the&nbsp;<em>ps<\/em>&nbsp;documentation, by default the output is not sorted. In that case either my kernel&#39;s process table is remarkably well organised, or the distributions I use &quot;cheat&quot; and sort the output in process ID order. In the distant past where computers were shared amongst too many people, and the machines themselves were quite slow, it made sense for the output of&nbsp;<em>ps<\/em>&nbsp;to be unsorted. But it certainly doesn&#39;t make sense now.\n<\/p>\n<p>\n\tAnd the&nbsp;<em>ps<\/em>&nbsp;command allows processes to be sorted by any field that you can specify in the &quot;STANDARD FORMAT SPECIFIERS&quot; section which conveniently enough you are now intimately acquianted. Simply add the relevant field to the &#8211;sort option :-\n<\/p>\n<pre>\r\n% <strong>ps --sort pcpu<\/strong>\r\n&nbsp; PID COMMAND &nbsp; &nbsp; &nbsp; &nbsp; %CPU %MEM NLWP USER &nbsp; &nbsp; STAT P WCHAN &nbsp;CLS PRI &nbsp;NI F\r\n31092 zsh &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.0 &nbsp;0.0 &nbsp; &nbsp;1 mike &nbsp; &nbsp; Ss &nbsp; * - &nbsp; &nbsp; &nbsp;TS &nbsp; 19 &nbsp; 0 0\r\n31743 ps &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.0 &nbsp;0.0 &nbsp; &nbsp;1 mike &nbsp; &nbsp; R+ &nbsp; 5 - &nbsp; &nbsp; &nbsp;TS &nbsp; 19 &nbsp; 0 0\r\n<\/pre>\n<p>\n\tWith just a short list (and such a low percentage of the cpu in use) it doesn&#39;t make sense, but added to -e, it does.\n<\/p>\n<p>\n\tRather than change the default sort order, I personally prefer to configure aliases to do the job for me :-\n<\/p>\n<pre>\r\n% <strong>alias&nbsp;pscpu=&#39;ps --sort pcpu&#39;<\/strong>\r\n% <strong>alias&nbsp;psmem=&#39;ps --sort pmem&#39;<\/strong>\r\n<\/pre>\n<p>\n\tPreferring to use an alias here is rather convenient as there doesn&#39;t seem to be a way to configure the default sort order &#8211; officially there isn&#39;t one!\n<\/p>\n<p>\n\tReading through the&nbsp;<em>ps<\/em>&nbsp;manual page (during which you will notice many different options referring to old Unix varients) is a reminder of just how long and bitter the fight over which&nbsp;<em>ps<\/em>&nbsp;varient was the best. And now for a completely irrelevant picture :-\n<\/p>\n<p>\n\t<a href=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"damascus-unix-prompt\" class=\"aligncenter size-full wp-image-3856\" height=\"463\" src=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt.png?resize=695%2C463\" width=\"695\" srcset=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt.png?w=792&amp;ssl=1 792w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wondered if you can tinker with the&nbsp;ps&nbsp;command to change how and what is displayed? No? Well give up reading this post then. I&#39;ve known about&nbsp;ps&nbsp;for ages and also the way that the output can be tinkered with, but have not had an excuse to dig into it properly until I was looking <a href='https:\/\/really.zonky.org\/?p=3857' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0"},"categories":[4,209],"tags":[43,1354,1353,649],"class_list":["post-3857","post","type-post","status-publish","format-standard","hentry","category-it","category-linux-it","tag-linux","tag-processes","tag-ps","tag-unix","category-4-id","category-209-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1f2KI-10d","_links":{"self":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/3857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3857"}],"version-history":[{"count":7,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/3857\/revisions"}],"predecessor-version":[{"id":3864,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/3857\/revisions\/3864"}],"wp:attachment":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}