0

To get all child process in a tree format, you can run the below

$ ps -g 24154 --forest
  PID TTY          TIME CMD
  24154 pts/4    00:00:00 bash
  25745 pts/4    00:00:00  \_ ps

How can I get the below o/p for a PID? Below is the partial o/p of ps -ef --forest | less for a PID 24154.

root      1173     1  0 Sep07 ?        00:00:00 /usr/sbin/sshd -D
root     24141  1173  0 11:10 ?        00:00:00  \_ sshd: vishwa [priv]
vishwa   24149 24141  0 11:10 ?        00:00:00  |   \_ sshd: vishwa@pts/4
vishwa   24154 24149  0 11:10 pts/4    00:00:00  |       \_ -bash
vishwa   26004 24154  0 11:22 pts/4    00:00:00  |           \_ ps -ef --forest
vishwa   26005 24154  0 11:22 pts/4    00:00:00  |           \_ less

1 Answer 1

2

You can get the process name and PID starting from the init process by running

$  pstree -ps $$
systemd(1)───sshd(1173)───sshd(42546)───sshd(42555)───bash(42560)───pstree(46455)

The below command worked as I did not have any child process running inside bash. If there are multiple jobs running inside the shell, you will not see the intended results

$ ps -ef --forest | grep -B3 $$
root      1173     1  0 Sep07 ?        00:00:00 /usr/sbin/sshd -D
root     42546  1173  0 20:44 ?        00:00:00  \_ sshd: vishwa [priv]
vishwa   42555 42546  0 20:44 ?        00:00:00  |   \_ sshd: vishwa@pts/3
vishwa   42560 42555  0 20:44 pts/3    00:00:00  |       \_ -bash
vishwa   46522 42560  0 21:13 pts/3    00:00:00  |           \_ ps -ef --forest
vishwa   46523 42560  0 21:13 pts/3    00:00:00  |           \_ grep --color=auto -B3 42560

$$ here is the PID of the current shell, type echo $$ to output the PID.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .