Last active
October 21, 2020 10:14
-
-
Save jul/c8b75d70a92079f9ad84fd92d17c5f58 to your computer and use it in GitHub Desktop.
watchdog 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
#!/usr/bin/bash | |
MYPID=$$ | |
watchdog () { | |
( PID=$1 | |
DONE=0 | |
while [[ $DONE == 0 ]]; do | |
echo "watchdog running" | |
if $( kill -0 $PID ); then | |
sleep 1; | |
else | |
kill -s SIGUSR1 $MYPID; | |
DONE=1; | |
fi | |
done ) & | |
}; | |
RUNNING="RUNNING" | |
watchdog $( pgrep this.sh ) | |
on_end () { echo "Singal launched"; RUNNING="STOPPED"; } | |
trap on_end SIGUSR1 | |
while [ 1 ]; do | |
sleep 1; | |
echo "JOB is " $RUNNING | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment