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.
kill-child-jobs/dash.sh

18 lines
395 B

#!/bin/sh
set -e
kill_child_jobs() {
echo "In kill_child_jobs()..."
# Workaround for https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482999
tmp="$(mktemp)"
jobs -p > "$tmp"
child_pids=$(cat "$tmp")
for child in $child_pids
do
echo Killing "$child"
# From https://stackoverflow.com/a/11231970
kill "$child" || true
done
rm "$tmp"
}