{"id":6238,"date":"2021-09-09T13:51:28","date_gmt":"2021-09-09T18:51:28","guid":{"rendered":"https:\/\/baselines.com\/?p=6238"},"modified":"2021-09-09T13:51:28","modified_gmt":"2021-09-09T18:51:28","slug":"elegantly-get-list-of-descendant-processes","status":"publish","type":"post","link":"https:\/\/baselines.com\/?p=6238","title":{"rendered":"Elegantly get list of descendant processes"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><\/h1>\n\n\n\n<p>Is there any command, or any simpler way to get the full list of\u00a0<em>all<\/em>\u00a0descendant processes?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8 Answers<\/h2>\n\n\n\n<p><a href=\"https:\/\/unix.stackexchange.com\/questions\/67668\/elegantly-get-list-of-descendant-processes?answertab=active#tab-top\" target=\"_blank\" rel=\"noopener\">Active<\/a><a href=\"https:\/\/unix.stackexchange.com\/questions\/67668\/elegantly-get-list-of-descendant-processes?answertab=oldest#tab-top\" target=\"_blank\" rel=\"noopener\">Oldest<\/a><a href=\"https:\/\/unix.stackexchange.com\/questions\/67668\/elegantly-get-list-of-descendant-processes?answertab=votes#tab-top\" target=\"_blank\" rel=\"noopener\">Votes<\/a><a><\/a>20<a href=\"https:\/\/unix.stackexchange.com\/posts\/83008\/timeline\" target=\"_blank\" rel=\"noopener\"><\/a><\/p>\n\n\n\n<p>The following is somewhat simpler, and has the added advantage of ignoring numbers in the command names:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                                                                              pstree -p $pid | grep -o '(&#91;0-9]\\+)' | grep -o '&#91;0-9]\\+'\n<\/code><\/pre>\n\n\n\n<p>Or with Perl:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                                                                              pstree -p $pid | perl -ne 'print \"$1\\n\" while \/\\((\\d+)\\)\/g'\n<\/code><\/pre>\n\n\n\n<p>We&#8217;re looking for numbers within parentheses so that we don&#8217;t, for example, give 2 as a child process when we run across&nbsp;<code>gif2png(3012)<\/code>. But if the command name contains a parenthesized number, all bets are off. There&#8217;s only so far text processing can take you.<\/p>\n\n\n\n<p>So I also think that process groups are the way to go. If you&#8217;d like to have a process run in its own process group, you can use the &#8216;pgrphack&#8217; tool from the Debian package &#8216;daemontools&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                                                                              pgrphack my_command args\n<\/code><\/pre>\n\n\n\n<p>Or you could again turn to Perl:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                                                                              perl -e 'setpgid or die; exec { $ARGV&#91;0] } @ARGV;' my_command args\n<\/code><\/pre>\n\n\n\n<p>The only caveat here is that process groups do not nest, so if some process is creating its own process groups, its subprocesses will no longer be in the group that you created.<a href=\"https:\/\/unix.stackexchange.com\/a\/83008\" target=\"_blank\" rel=\"noopener\">Share<\/a><a href=\"https:\/\/unix.stackexchange.com\/posts\/83008\/edit\" target=\"_blank\" rel=\"noopener\">Improve this answer<\/a>Followanswered&nbsp;Jul 14 &#8217;13 at 19:01<a href=\"https:\/\/unix.stackexchange.com\/users\/3629\/jander\" target=\"_blank\" rel=\"noopener\"><\/a><a href=\"https:\/\/unix.stackexchange.com\/users\/3629\/jander\" target=\"_blank\" rel=\"noopener\">Jander<\/a><strong>14.7k<\/strong>55 gold badges4343 silver badges6565 bronze badges<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Child processes are arbitrary and may or may not use process groups themselves (I cannot assume anything). However your answer comes the closest to what seesm to be achievable in Linux, so I&#8217;ll accept it. Thanks.&nbsp;\u2013&nbsp;<a href=\"https:\/\/unix.stackexchange.com\/users\/29319\/stenyak\" target=\"_blank\" rel=\"noopener\">STenyaK<\/a>&nbsp;<a href=\"https:\/\/unix.stackexchange.com\/questions\/67668\/elegantly-get-list-of-descendant-processes#comment123633_83008\" target=\"_blank\" rel=\"noopener\">Jul 17 &#8217;13 at 11:57<\/a><\/li><li>This was very useful !&nbsp;\u2013&nbsp;<a href=\"https:\/\/unix.stackexchange.com\/users\/152260\/michal-gallovic\" target=\"_blank\" rel=\"noopener\">Michal Gallovic<\/a>&nbsp;<a href=\"https:\/\/unix.stackexchange.com\/questions\/67668\/elegantly-get-list-of-descendant-processes#comment453077_83008\" target=\"_blank\" rel=\"noopener\">Feb 11 &#8217;16 at 20:43<\/a><\/li><li>The pstree pipes will also include the thread ids, i.e. the IDs of the threads a $pid has started.&nbsp;\u2013&nbsp;<a href=\"https:\/\/unix.stackexchange.com\/users\/1131\/maxschlepzig\" target=\"_blank\" rel=\"noopener\">maxschlepzig<\/a>&nbsp;<a href=\"https:\/\/unix.stackexchange.com\/questions\/67668\/elegantly-get-list-of-descendant-processes#comment599175_83008\" target=\"_blank\" rel=\"noopener\">Jan 21 &#8217;17 at 9:05<\/a><\/li><li>You can use single grep:&nbsp;<code>pstree -lp | grep -Po \"(?&lt;=\\()\\d+(?=\\))\"<\/code>&nbsp;\u2013&nbsp;<a href=\"https:\/\/unix.stackexchange.com\/users\/33942\/puchu\" target=\"_blank\" rel=\"noopener\">puchu<\/a>&nbsp;<a href=\"https:\/\/unix.stackexchange.com\/questions\/67668\/elegantly-get-list-of-descendant-processes#comment776608_83008\" target=\"_blank\" rel=\"noopener\">Mar 13 &#8217;18 at 21:48<\/a>&nbsp;<\/li><\/ul>\n\n<script>\nvar zbPregResult = '0';\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Is there any command, or any simpler way to get the full list of\u00a0all\u00a0descendant processes? 8 Answers ActiveOldestVotes20 The following is somewhat simpler, and has the added advantage of ignoring numbers in the command names: Or with Perl: We&#8217;re looking for numbers within parentheses so that we don&#8217;t, for example, give 2 as a child [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_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}},"categories":[272],"tags":[],"class_list":["post-6238","post","type-post","status-publish","format-standard","hentry","category-manuals-and-documents"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/posts\/6238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/baselines.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6238"}],"version-history":[{"count":1,"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/posts\/6238\/revisions"}],"predecessor-version":[{"id":6239,"href":"https:\/\/baselines.com\/index.php?rest_route=\/wp\/v2\/posts\/6238\/revisions\/6239"}],"wp:attachment":[{"href":"https:\/\/baselines.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baselines.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baselines.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}