21

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
  • 2
    Why not use pstree? – muru Feb 20 '16 at 9:50
  • @muru I tried pstree and couldn't get it to produce meaningful output, I think pstree $$ just produced bash--pstree not exactly what I was looking for. – the_velour_fog Feb 20 '16 at 9:53
  • How is it not what you're looking for? You have replaced the script and ps, so what else do you expect to see except for pstree? – muru Feb 20 '16 at 9:55
  • @muru your right its technically what I wanted, but too minimal. i.e. how do you know for sure which processes you are looking at without a PID like ps shows it it table output? – the_velour_fog Feb 20 '16 at 10:03
  • 4
    pstree -p $$? Or, if you want more of the command line show, pstree -pa $$. Or, if you want to show all parent processes going up, pstree -psa $$. – muru Feb 20 '16 at 10:04
20

I found it after reading this superuser answer, noting this comment

But not for a PID (-p) because it prints only the specific process, but for the session (-g)

and experimenting

ps f -g<PID>

result

$ ./script.sh $$
  PID TTY      STAT   TIME COMMAND
14492 pts/24   Ss     0:00 -bash
 9906 pts/24   S+     0:00  \_ bash ./script.sh 14492
 9907 pts/24   R+     0:00      \_ ps f -g14492
  • 3
    from the man page: OUTPUT MODIFIERS: f ASCII-art process hierarchy (forest) – phyatt Feb 16 '17 at 15:33
18

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
  • 5
    The question implied looking for a process tree starting at a specific process, taking $1 argument to a script or using $$ to look at the tree starting at the current shell... Can you update your answer to include information on how to get a forest starting at a specific process? – filbranden Apr 9 '18 at 18:42
1

You can use the command ps f -g <PID> and stat the root process for PID:

#> ps f -g 0

PID TTY      STAT   TIME COMMAND
2 ?        S      0:00 [kthreadd]
3 ?        S      0:01  \_ [ksoftirqd/0]
7 ?        S      0:19  \_ [rcu_sched]
0

Try this:

 $ ps -afx
  PID TTY      STAT   TIME COMMAND
    2 ?        S      0:00 [kthreadd]
    4 ?        I<     0:00  \_ [kworker/0:0H]
    6 ?        I<     0:00  \_ [mm_percpu_wq]
    7 ?        S      0:14  \_ [ksoftirqd/0]
    8 ?        I      0:34  \_ [rcu_sched]
    9 ?        I      0:00  \_ [rcu_bh]
   10 ?        S      0:00  \_ [migration/0]
   11 ?        S      0:00  \_ [watchdog/0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.