How can I log a linux command's process tree?

wsaxton asked:

I’m trying to track the usage of a certain command so I want to log its parents (to see what is actually using it)

Solaris has “ptree” which would be pretty easy to parse and log:

# ptree 4198
4198  /usr/bin/perl -S /sample.pl
  4228  /usr/bin/java -server -cp /sample.java

Linux doesn’t seem to have an equivalent. I’ve tried using “pstree” and “ps -auxf” and others without much success.

My answer:


pstree seems fine for this.

Consider:

$ pstree -p 3281
nginx(3281)─┬─nginx(3282)
            ├─nginx(3283)
            ├─nginx(3284)
            ├─nginx(3285)
            ├─nginx(3286)
            └─nginx(3287)

And use -s to see parents:

$ pstree -p -s 3287
systemd(1)───nginx(3281)───nginx(3287)

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.