4

(TLDR Doug O'Neal's strace -f -e execve ./myprogram solved me problem)

I start a program on the command line. The executing program spawns processes. I would like to see or log the names of all processes spawned by the program.

Details

I've tried top with forest view V:

$ top -c -d 1

Only the parent program is briefly visible. I suspect the refresh rate is too slow to show the child processes.

I've tried to filter by COMMAND=myprogram but this most probably filters out the child processes.

The parent program spawns processes but I am not sure if top's forest view will show those new processes indented under the original process or independently. I'm not sure if a spawned process can be independent of the parent.

Update #1

I tried this answer with the sleep removed.

#!/bin/bash

mkdir -p "$HOME/ps_logs"

while true; do
    ps faux > "$HOME/ps_logs/ps_$(date +%Y-%m-%d_%H:%M:%S).log"
done

I filtered the output with:

grep -rnw './' -e 'myprogam'

All the files only contain myprogram. So how do I know if myprogram even spawns any processes?

robor
  • 163
  • 2
  • 8
  • 1
    Have you tried `pstree`? – steeldriver Jul 05 '18 at 12:08
  • How should I employ pstree? The program I'm analysing finishes in under a second. pstree shows running processes. I'm looking for something that logs the spawned processes as the program runs. – robor Jul 05 '18 at 12:13
  • 1
    Well you could background the parent, and use `$!` to immediately get its PID to pass to pstree e.g. `./yourprog & pstree -p $!` – steeldriver Jul 05 '18 at 12:24
  • @steeldriver I tried that. The output is user-> ./myprog & pstree -p $! [2] 25769 myprog(25769) user-> pstree -p 25769 [2]+ Done ./myprog – robor Jul 05 '18 at 12:37
  • 5
    Maybe `strace -f -e execve ./myprogram` ? Doesn't have the problem of trying to shell out another command before your program actually exits. – doneal24 Jul 05 '18 at 12:53
  • 1
    Yeah or see [How to track child process using strace?](https://stackoverflow.com/questions/4053142/how-to-track-child-process-using-strace) – steeldriver Jul 05 '18 at 12:56
  • Related - https://unix.stackexchange.com/questions/124127/kill-all-descendant-processes/124129#124129. – slm Jul 05 '18 at 14:59

1 Answers1

4

I generally like using ps auxf because it visually shows the child processes under the parent visually:

$ ps auxf
...
root       637  0.0  0.0 110044   800 tty1     Ss+  02:50   0:00 /sbin/agetty --noclear tty1 linux
root       983  0.0  0.1 404028  1136 ?        Sl   02:50   0:11 /usr/sbin/VBoxService --pidfile /var/run/vboxadd-service.sh
root      1013  0.0  1.6 562416 16444 ?        Ssl  02:50   0:03 /usr/bin/python -Es /usr/sbin/tuned -l -P
root      1015  0.0  0.4 105996  4108 ?        Ss   02:50   0:00 /usr/sbin/sshd -D
root     20191  0.0  0.5 152116  5576 ?        Ss   10:06   0:00  \_ sshd: vagrant [priv]
vagrant  20193  0.0  0.2 152304  2872 ?        S    10:06   0:00      \_ sshd: vagrant@pts/0
vagrant  20194  0.0  0.2 115964  2644 pts/0    Ss   10:06   0:00          \_ -bash
root     20232  0.0  0.2 201844  2956 pts/0    S    10:06   0:00              \_ sudo -Es
root     20233  0.0  0.2 116208  2964 pts/0    S    10:06   0:00                  \_ /bin/bash
root     20510  0.0  0.1 151240  1932 pts/0    R+   11:01   0:00                      \_ ps auxf
root      1115  0.0  0.2  91628  2192 ?        Ss   02:50   0:00 /usr/libexec/postfix/master -w
postfix   1117  0.0  0.3  91800  4048 ?        S    02:50   0:00  \_ qmgr -l -t unix -u
postfix  20149  0.0  0.3  91776  4048 ?        S    09:39   0:00  \_ pickup -l -t unix -u
...

Additionally if you just want to view the list of PID + PGID you can use ps with these switches like so:

$ ps x -o  "%p %r %c"
  PID  PGID COMMAND
    1     1 systemd
    2     0 kthreadd
    3     0 ksoftirqd/0
    5     0 kworker/0:0H
    7     0 migration/0
    8     0 rcu_bh
  ...
  ...
  591   591 rngd
  594   594 systemd-logind
  596   596 smartd
  597   597 rsyslogd
  600   600 acpid
  616   616 abrtd
  617   617 abrt-watch-log
  630   630 atd
  637   637 agetty
  983   981 VBoxService
 1013  1013 tuned
 1015  1015 sshd
 1115  1115 master
 2426  2426 NetworkManager
 2439  2439 dhclient
 3123  3123 firewalld
 3828     0 kworker/u2:1
slm
  • 363,520
  • 117
  • 767
  • 871