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 |
@kool927 what was the full output? How did you stop/kill the program?
It was printing in loop this
the program exited with code 0
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
Can you provide a manual on how to use this? I tried by creating a bash file
bash.sh
and then attach my program to it likebash.sh my_program.go
It didn't work when I stopped the program