Skip to content

Instantly share code, notes, and snippets.

@bmatthewshea
Created October 17, 2017 14:49
Show Gist options
  • Save bmatthewshea/e437fb73379f2598b5b983a635a00933 to your computer and use it in GitHub Desktop.
Save bmatthewshea/e437fb73379f2598b5b983a635a00933 to your computer and use it in GitHub Desktop.
A BASH script to monitor a log and restart a service if idle.
#!/bin/bash
set -x
PATH=$PATH:/opt/scripts
SERVICE=ethminer
# start infinite loop
while :
do
SECS=$(date +%s)
idletime=$(expr $SECS - $(date +%s -r /tmp/ethminer.log))
if [ "$idletime" -gt 120 ]
then
echo "Restarting service due to no activity for 2 minutes! ($idletime seconds)"
sudo service $SERVICE restart # restart or send mail etc here
sleep 60
fi
# loop repeats until ctrl-c:
sleep 30
done
@bmatthewshea
Copy link
Author

Execute with sudo or as root to put the script in background:
screen -dmSL log-monitor ./restart-svc-on-log-failure.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment