{"id":4277,"date":"2018-06-01T19:34:52","date_gmt":"2018-06-01T15:34:52","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-pstree-command\/"},"modified":"2018-06-01T19:34:52","modified_gmt":"2018-06-01T15:34:52","slug":"linux-pstree-command-tutorial-for-beginners-8-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-pstree-command-tutorial-for-beginners-8-examples\/","title":{"rendered":"Linux pstree Command Tutorial for Beginners (8 Examples)"},"content":{"rendered":"<p>Since every process (except the very first one) in a Linux system has a parent, it sometimes makes things easier to understand if all processes are displayed in a tree structure. You&#8217;ll be glad to know there exists a command line utility &#8211; dubbed <strong>pstree<\/strong> &#8211; that displays a tree of processes.<\/p>\n<p>In this tutorial, we will discuss the basics of pstree command using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 16.04 LTS machine.<\/p>\n<h2 id=\"linux-pstree-command\">Linux pstree command<\/h2>\n<p>As already mentioned in the beginning, the pstree command displays a tree of processes. Following is its syntax:<\/p>\n<p class=\"command\">pstree [options]<\/p>\n<p>And here&#8217;s what the tool&#8217;s man page says about it:<\/p>\n<pre readability=\"30\"> pstree shows running processes as a tree.\u00a0 The tree is rooted at either<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pid\u00a0 or\u00a0 init\u00a0 if\u00a0 pid\u00a0 is\u00a0 omitted.\u00a0\u00a0 If a user name is specified, all<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 process trees rooted at processes owned by that user are shown.<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pstree visually merges identical branches by\u00a0 putting\u00a0 them\u00a0 in\u00a0 square<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 brackets and prefixing them with the repetition count, e.g.<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 init-+-getty<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |-getty<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |-getty<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 `-getty<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 becomes<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 init---4*[getty]<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Child\u00a0 threads\u00a0 of a process are found under the parent process and are<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 shown with the process name in curly braces, e.g.<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 icecast2---13*[{icecast2}]<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 If pstree is called as pstree.x11 then it will prompt the user\u00a0 at\u00a0 the<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 end of the line to press return and will not return until that has hap?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pened.\u00a0 This is useful for when pstree is run in a xterminal.<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Certain kernel or mount parameters, such\u00a0 as\u00a0 the\u00a0 hidepid\u00a0 option\u00a0 for<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 procfs,\u00a0 will\u00a0 hide information for some processes. In these situations<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pstree will attempt to build the tree without this information, showing<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 process names as question marks.<\/p><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the pstree command works:<\/p>\n<h2 id=\"q-how-to-use-pstree-command\">Q1. How to use pstree command?<\/h2>\n<p>Basic usage is simple: all you have to do is to execute &#8216;pstree&#8217; sans any option.<\/p>\n<p class=\"command\">pstree<\/p>\n<p><a class=\"fancybox\" id=\"img-pstree-basic\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pstree-basic.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples.png\" alt=\"How to use pstree command\" width=\"500\" height=\"526\" title=\"\"><\/a><\/p>\n<p>So you can see this is how pstree produces process-related information in output.<\/p>\n<h2 id=\"q-how-to-make-pstree-include-command-line-arguments-in-output-as-well\">Q2. How to make pstree include command line arguments in output as well?<\/h2>\n<p>This can be done using the -a command line option.<\/p>\n<p class=\"command\">pstree -a<\/p>\n<p><a class=\"fancybox\" id=\"img-pstree-a-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pstree-a-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-1.png\" alt=\"How to make pstree include command line arguments in output as well\" width=\"500\" height=\"513\" title=\"\"><\/a><\/p>\n<p>So you can see the pstree command now also displays command line options for some processes.<\/p>\n<h2 id=\"q-how-to-force-pstree-to-expand-identical-subtrees-in-output\">Q3. How to force pstree to expand identical subtrees in output?<\/h2>\n<p>By default, the pstree command merges identical branches by putting them in square brackets and prefixing them with the repetition count. Something like this:<\/p>\n<p><a class=\"fancybox\" id=\"img-pstree-merge-default\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pstree-merge-default.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-2.png\" alt=\"How to force pstree to expand identical subtrees in output\" width=\"166\" height=\"41\" title=\"\"><\/a><\/p>\n<p>However, if you want, you can force the tool to expand identical trees, something you can do using the -c command line option.<\/p>\n<p class=\"command\">pstree -c<\/p>\n<p><a class=\"fancybox\" id=\"img-pstree-c-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pstree-c-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-3.png\" alt=\"Expanded subtrees\" width=\"165\" height=\"88\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-make-pstree-highlight-a-specific-process\">Q4. How to make pstree highlight a specific process?<\/h2>\n<p>In case you want the tool to highlight a specific process in output, use the -H command line option.<\/p>\n<p class=\"command\">pstree -H [PID]<\/p>\n<p>Where PID is the ID of the process you want to highlight. For example, I highlighted the firefox process on my system using the following command:<\/p>\n<p class=\"command\">pstree -H 3124<\/p>\n<p><a class=\"fancybox\" id=\"img-pstree-H-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pstree-H-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-4.png\" alt=\"How to make pstree highlight a specific process\" width=\"406\" height=\"116\" title=\"\"><\/a><\/p>\n<p>So you can see the name &#8216;firefox&#8217; is slightly highlighted compared to other names.<\/p>\n<h2 id=\"q-how-make-pstree-show-process-group-ids-in-output\">Q5. How make pstree show process group IDs in output?<\/h2>\n<p>For this, use the -g command line option.<\/p>\n<p class=\"command\">pstree -g<\/p>\n<p><a class=\"fancybox\" id=\"img-pstree-g\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pstree-g.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-5.png\" alt=\"How make pstree show process group IDs in output\" width=\"454\" height=\"525\" title=\"\"><\/a><\/p>\n<p>So you can see that process group IDs are shown as decimal numbers in parentheses after each process name.<\/p>\n<h2 id=\"q-how-to-make-pstree-sort-processes-based-on-pids\">Q6. How to make pstree sort processes based on PIDs?<\/h2>\n<p>By default, pstree sorts processes with same ancestor by name. However, if you want, you can have pstree sort processes by PIDs as well, something which you can do using the -n command line option.<\/p>\n<p class=\"command\">pstree -n<\/p>\n<p>Note that this type of sorting is also known as numeric sort.<\/p>\n<h2 id=\"q-how-to-make-pstree-display-process-tree-specific-to-a-user\">Q7. How to make pstree display process tree specific to a user?<\/h2>\n<p>If you want pstree to display all process trees rooted at processes owned by a specific user, then all you have to do is to pass the name of that user as input to the command.<\/p>\n<p>For example,<\/p>\n<p class=\"command\">pstree himanshu<\/p>\n<p>So in this case, following output was produced on my system:<\/p>\n<p><a class=\"fancybox\" id=\"img-pstree-user\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pstree-user.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-6.png\" alt=\"How to make pstree display process tree specific to a user\" width=\"500\" height=\"550\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-restrict-pstree-to-a-specific-process\">Q8. How to restrict Pstree to a specific process?<\/h2>\n<p>If you want pstree to display only the parent and child info for a specific process, use the -s option.<\/p>\n<p class=\"command\">pstree -s [PID]<\/p>\n<p>For example, I wanted to limit the Pstree output to only the firefox process on my system, so I executed the following command:<\/p>\n<p class=\"command\">pstree -s 3124<\/p>\n<p>And here&#8217;s the output the tool displayed:<\/p>\n<p><a class=\"fancybox\" id=\"img-pstree-s-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/pstree-s-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-7.png\" alt=\"How to restrict Pstree to a specific process\" width=\"500\" height=\"463\" title=\"\"><\/a><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Depending on the kind of work you do, the pstree command can be of great help. Here, in this tutorial, we have discussed some key command line options this tool offers. For more info, head to its <a href=\"https:\/\/linux.die.net\/man\/1\/pstree\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-pstree-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-8.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-pstree-command%2F&amp;text=Linux+pstree+Command+Tutorial+for+Beginners+%288+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-9.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-10.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-pstree-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/linux-pstree-command-tutorial-for-beginners-8-examples-11.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Since every process (except the very first one) in a Linux system has a parent, it sometimes makes things easier to understand if all processes are displayed in a tree structure. You&#8217;ll be glad to know there exists a command line utility &#8211; dubbed pstree &#8211; that displays a tree of processes. In this tutorial, &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-4277","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=4277"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4277\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=4277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=4277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=4277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}