Sometimes I have apps that suddenly stop working, however they don’t have PID output or I can’t start them via systemd or upstart due to convoluted requirements. Other times the app is on but it stops processing incoming queues due to various reasons. I need to make sure i have a mechanism in place to┬áRestart service as needed. I’m going to describe how you can check for Linux logs output and if there is no log for X seconds then we restart the app! we check the log every X seconds and if in the past X seconds there is no movement we perform X function.

Restart service script

#!/bin/bash
# simple script to check logs, if no entries have been made in 45 seconds restart
set -x
PATH=$PATH:/usr/sbin

TS=$(date +%s)

timer1=$(expr $TS - $(date +%s -r /path/to/some/file.log)) # make sure to set the log path here

if [ "$timer1" -gt 45 ]
then
        echo $timer1
        echo "restarting service due to no activity for 45 seconds"
        sudo service mycoolservice restart    # you can change this for something else such as sending email or even rebooting the machine.
fi

Leave a comment

Leave a Reply or the kitten gets it!