Initial version of script working on bash, dash, zsh, and ksh.
This commit is contained in:
parent
03f1ba07c7
commit
849a02481d
54
all.sh
Executable file
54
all.sh
Executable file
|
@ -0,0 +1,54 @@
|
|||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
# From https://stackoverflow.com/a/360275
|
||||
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
|
||||
}
|
||||
|
||||
#trap "kill_child_jobs" INT TERM QUIT
|
||||
#trap date INT TERM QUIT
|
||||
|
||||
# From https://unix.stackexchange.com/a/240736
|
||||
for sig in INT QUIT HUP TERM ALRM USR1; do
|
||||
trap "
|
||||
cleanup
|
||||
trap - $sig EXIT
|
||||
kill -s $sig "'"$$"' "$sig"
|
||||
done
|
||||
trap cleanup EXIT
|
||||
|
||||
yes aa &
|
||||
yes b &
|
||||
sleep 100 &
|
||||
|
||||
wait
|
Loading…
Reference in New Issue
Block a user