Skip to content

Instantly share code, notes, and snippets.

@jul
Last active October 21, 2020 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jul/c8b75d70a92079f9ad84fd92d17c5f58 to your computer and use it in GitHub Desktop.
Save jul/c8b75d70a92079f9ad84fd92d17c5f58 to your computer and use it in GitHub Desktop.
watchdog in bash
#!/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