This is my solution using job control.
- Whenever a job dies, it kills the parent script
- When the parent script dies, it kill the whole process group, including all jobs
I would hope that there's something simpler out there.
#!/usr/bin/env bash
# Mocks
function process () {
while true ; do
echo "Process $1 is working..."
sleep 10
done
}
# Actual implementation
trap terminate_entire_process_group EXIT
function terminate_entire_process_group () {
trap - EXIT
kill -15 -$$
}
function terminate_parent_process () {
trap - EXIT
kill $$ 2> /dev/null
}
(
trap terminate_parent_process EXIT
process 1
) &
(
trap terminate_parent_process EXIT
process 2
) &
wait