How to get list of all child process spawned by a script
To answer your question directly, the command
jobs -p
gives you the list of all child processes.
Alternative #1
But in your case it might be easier to just use the command wait
without any params:
first_program &
second_program &
wait
This will wait until ALL child processes have finished.
Alternative #2
Another alternative is using $!
to get the PID of the last program and perhaps accumulate in a variable, like so:
pids=""
first_program &
pids="$pids $!"
second_program &
pids="$pids $!"
and then use wait with that (this is in case you only want to wait for a subset of you child processes):
wait $pids
Alternative #3
OR, if you want to wait only until ANY process has finished, you can use
wait -n $pids
Bonus info
If you want a sigterm to your bash script to close your child processes as well, you will need to propagate the signal with something like this (put this somewhere at the top, before starting any process):
trap 'kill $(jobs -p)' SIGINT SIGTERM EXIT