Examples of how to kill all child jobs when a shell script exits.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
395 B

  1. #!/bin/sh
  2. set -e
  3. kill_child_jobs() {
  4. echo "In kill_child_jobs()..."
  5. # Workaround for https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482999
  6. tmp="$(mktemp)"
  7. jobs -p > "$tmp"
  8. child_pids=$(cat "$tmp")
  9. for child in $child_pids
  10. do
  11. echo Killing "$child"
  12. # From https://stackoverflow.com/a/11231970
  13. kill "$child" || true
  14. done
  15. rm "$tmp"
  16. }