1
0
kill-child-jobs/all-full-example.sh

51 lines
1.1 KiB
Bash
Executable File

#!/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
# From https://unix.stackexchange.com/a/574169
if [ -n "${child##*[!0-9]*}" ]
then
echo Killing "$child"
# From https://stackoverflow.com/a/11231970
kill "$child" || true
fi
done
# zsh version from https://stackoverflow.com/a/36354483
# zsh doesn't have jobs -p
jobs > "$tmp"
cat "$tmp"
<"$tmp" sed --regexp-extended --quiet 's/\[([[:digit:]]+)\].*running.*/%\1/gp' | while read -r child
do
echo Killing job "$child"
kill "$child" || true
done
rm "$tmp"
}
cleanup() {
kill_child_jobs
}
# From https://unix.stackexchange.com/a/240736
for sig in INT QUIT HUP TERM; do
trap "
cleanup
trap - $sig EXIT
kill -s $sig "'"$$"' "$sig"
done
trap cleanup EXIT
yes aa &
yes disowned & builtin disown || true
sleep 100 &
wait