Shell – How to show a terminal shell’s process tree including children

psshell

When a script is launched from command prompt the shell will spawn a subprocess for that script. I want to show that relationship between terminal level process and its children using ps in a tree style output.
How can I do this?

What I have tried so far

file: script.sh

#!/bin/bash

ps -f -p$1

Then I invoke the script from the command line passing in the process id of the terminal shell:

$ ./script.sh $$

What I want is something like this

  • top level (terminal) shell process
  • ./script.sh
  • process for ps command itself
USER    PID  [..]
ubuntu 123     -bash
ubuntu 1234    \_ bash ./script.sh
ubuntu 12345      \_ ps auxf 

what Im getting is:

  PID TTY      STAT   TIME COMMAND
14492 pts/24   Ss     0:00 -bash

Best Answer

Try

# ps -aef --forest
root     114032   1170  0 Apr05 ?        00:00:00  \_ sshd: root@pts/4
root     114039 114032  0 Apr05 pts/4    00:00:00  |   \_ -bash
root      56225 114039  0 13:47 pts/4    00:00:16  |       \_ top
root     114034   1170  0 Apr05 ?        00:00:00  \_ sshd: root@notty
root     114036 114034  0 Apr05 ?        00:00:00  |   \_ /usr/libexec/openssh/sftp-server
root     103102   1170  0 Apr06 ?        00:00:03  \_ sshd: root@pts/0
root     103155 103102  0 Apr06 pts/0    00:00:00  |   \_ -bash
root     106798 103155  0 Apr06 pts/0    00:00:00  |       \_ su - postgres
postgres 106799 106798  0 Apr06 pts/0    00:00:00  |           \_ -bash
postgres  60959 106799  0 14:39 pts/0    00:00:00  |               \_ ps -aef --forest
postgres  60960 106799  0 14:39 pts/0    00:00:00  |               \_ more
Related Question