How to propagate a signal to child processes from a Bash script - LinuxConfig.org

Suppose we write a script which spawns one or more long running processes; if said script receives a signal such as SIGINT or SIGTERM, we probably want its children to be terminated too (normally when the parent dies, the children survives). We may also want to perform some cleanup tasks before the script itself exits. To be able to reach our goal, we must first learn about process groups and how to execute a process in background.

This is a companion discussion topic for the original entry at https://linuxconfig.org/how-to-propagate-a-signal-to-child-processes-from-a-bash-script

Hello and thanks for that really informative and well explained post! When I tried out the example myself I stumbled upon the following I couldn’t explain to myself. If I start the sleep 30 & together with wait $! and I call ps -a -o pid,ppid,pgid,cmd, it tells me, that the calling script and the sleep command are in the same pgid. I would’ve expected that sleep is in a different pgid than the calling script as I thought sleep is a background process due to the & whereas the script is a foreground process. What am I missing?