There is also the issue of correctness. Naively parsing the output of
pstree is problematic for several reasons:
- pstree displays PIDs and the ids of threads (names are shown in curly braces)
- a command name might contain curly braces, numbers in parentheses that make reliable parsing impossible
If you have Python and the
psutil package installed you can use this snippet to list all descendant processes:
pid=2235; python3 -c "import psutil
for c in psutil.Process($pid).children(True):
(The psutil package is e.g. installed as a dependency of the
tracer command which is available on Fedora/CentOS.)
Alternatively, you can do an breadth-first traversal of the process tree in a bourne shell:
ps=2235; while [ "$ps" ]; do echo $ps; ps=$(echo $ps | xargs -n1 pgrep -P); \
done | tail -n +2 | tr " " "\n"
For computing the transitive-closure of a pid, the tail part can be omitted.
Note that the above doesn't use recursion and also runs in ksh-88.
On Linux, one can eliminate the
pgrep call and instead read the information from
ps=2235; while [ "$ps" ]; do echo $ps ; \
ps=$(for p in $ps; do cat /proc/$p/task/$p/children; done); done \
| tr " " "\n"' | tail -n +2
This is more efficient because we save one fork/exec for each PID and
pgrep does some additional work in each call.