Shell methods
When you run a command in a shell you can find out the PID of the last process that was run in the background using the special variable $! in a Bash shell.
For example:
$ sleep 5 & $ echo $! 8648
Also when you background a process the PID of that process is returned via the console like this:
$ sleep 5 & [1] 8648
Another method would be to use pgrep -P <PPID>. For example:
# PID of bash shell $ echo $$ 8376 # run fake job $ sleep 120 & [1] 8891 # pgrep PPID $ pgrep -P 8376 8891
valgrind
There is another method if you can inject the following in front of the execution of the program you're trying to get the child PIDs to:
$ valgrind --trace-children=yes <cmd>
For example:
# sample.bash #/bin/bash ls $ valgrind --trace-children=yes ./sample.bash ... ==17734== possibly lost: 0 bytes in 0 blocks ==17734== still reachable: 33,606 bytes in 95 blocks ==17734== suppressed: 0 bytes in 0 blocks ==17734== Rerun with --leak-check=full to see details of leaked memory ==17734== ==17734== For counts of detected and suppressed errors, rerun with: -v ==17734== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6) ==17733== ==17733== HEAP SUMMARY: ==17733== in use at exit: 36,409 bytes in 879 blocks ...
The 17733 is the PPID and the PID 17734 is the child PID that get's invoked (ls).