Skip to content

Instantly share code, notes, and snippets.

@arouene
Last active September 7, 2022 20:40
Show Gist options
  • Save arouene/70159b6d3036733ca7aff85847ced348 to your computer and use it in GitHub Desktop.
Save arouene/70159b6d3036733ca7aff85847ced348 to your computer and use it in GitHub Desktop.
Interruptible process watcher
#!/bin/bash
declare -a JOBS
JOBS=("sleep 1" "sleep 2" "sleep 3")
cleanup() {
trap "" SIGTERM
kill 0
wait
echo "Exited"
exit 0
}
trap cleanup SIGINT SIGTERM
for job in "${JOBS[@]}"; do
(
# Always restart the jobs
while true; do
echo "Running: $job"
/bin/sh -c "$job"
# if a shell is not needed use `eval "$job"` instead
done
) &
done
wait
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment