How to find ancestor chain of a process?
I can do ps -o "pid,ppid,args" -p my_pid
to find the parent of my_pid
.
I can continue ps -o "pid,ppid,args" -p previously_found_ppid
in a loop until get to ppid=1
.
However, I bet, there is a single call solution that would start with my_pid
and list all its ancestors. Something reverse to pstree
that starts with the parent and list all its descendants. Could you tell whether such Unix/Linux command is readily available?
It looks like pstree
can do what you want, with it's -s
Show parent processes of the specified process option
$ pstree -s 5698
init───mdm───mdm───init───at-spi-bus-laun───dbus-daemon
Or more info (arguments) and prettier with the -a
option
mint@mint ~ $ pstree -s -a 5698
init
└─mdm
└─mdm
└─init --user
└─at-spi-bus-laun
└─dbus-daemon --config-file=/etc/at-spi2/accessibility.conf ...
Or a few options together
$ pstree -s -p -a -G -l 5698
init,1
└─mdm,1994
└─mdm,5358
└─init,5379 --user
└─at-spi-bus-laun,5691
└─dbus-daemon,5698 --config-file=/etc/at-spi2/accessibility.conf --nofork --print-address 3
pstree
can be used to print all the ancestors of a process. But latest Linux distributions uses systemd
instead of init
.
Example: pstree -p -s 6206
will give following output
systemd(1)───lightdm(1066)───lightdm(1191)───upstart(1360)───gnome-terminal-(5222)───bash(5229)───cpu-print(6206)
A systemd, may refer to all the packages, utilities and libraries around daemon. It was designed to overcome the shortcomings of init [1].
reference [1]:https://www.tecmint.com/systemd-replaces-init-in-linux/