Is there any command, or any simpler way to get the full list of all descendant processes?
8 Answers
The following is somewhat simpler, and has the added advantage of ignoring numbers in the command names:
pstree -p $pid | grep -o '([0-9]\+)' | grep -o '[0-9]\+'
Or with Perl:
pstree -p $pid | perl -ne 'print "$1\n" while /\((\d+)\)/g'
We’re looking for numbers within parentheses so that we don’t, for example, give 2 as a child process when we run across gif2png(3012)
. But if the command name contains a parenthesized number, all bets are off. There’s only so far text processing can take you.
So I also think that process groups are the way to go. If you’d like to have a process run in its own process group, you can use the ‘pgrphack’ tool from the Debian package ‘daemontools’:
pgrphack my_command args
Or you could again turn to Perl:
perl -e 'setpgid or die; exec { $ARGV[0] } @ARGV;' my_command args
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.ShareImprove this answerFollowanswered Jul 14 ’13 at 19:01Jander14.7k55 gold badges4343 silver badges6565 bronze badges
- 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’ll accept it. Thanks. – STenyaK Jul 17 ’13 at 11:57
- This was very useful ! – Michal Gallovic Feb 11 ’16 at 20:43
- The pstree pipes will also include the thread ids, i.e. the IDs of the threads a $pid has started. – maxschlepzig Jan 21 ’17 at 9:05
- You can use single grep:
pstree -lp | grep -Po "(?<=\()\d+(?=\))"
– puchu Mar 13 ’18 at 21:48