Created
November 14, 2016 20:29
-
-
Save dansimau/fd34c46dd9a21f4d364f47f30002df47 to your computer and use it in GitHub Desktop.
process supervisor in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Spawn a process and restart it if it exits. | |
# | |
declare -i exit_code | |
declare -i pid | |
_cleanup() { | |
echo "[$(date)]: sending TERM to pid $pid" | |
kill -TERM $pid | |
exit 0 | |
} | |
trap _cleanup EXIT | |
while true; do | |
echo "[$(date)]: spawning $1..." | |
"$@" & | |
pid=$! | |
wait $pid | |
exit_code=$? | |
echo "[$(date)]: $1 exited with code $exit_code" | |
sleep 1 | |
done |
Replacing "$@" to /bin/bash -c "$1" works as expected
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It was printing in loop this
the program exited with code 0