Script: herstart automatisch service


#1

Aan de hand van volgend scriptje kan je controleren of een welbepaade service draait, en zonodig herstarten.
Dit voorbeeld is specifiek voor zoneminder, welke 2 services nodig heeft om correct te draaien…

#!/bin/sh

#Put this script in your crontab, and it will simply check if zoneminder is running, and if not, it’ll try to restart it.
#Cron with fe 20 minutes should be enough

SERVICE=zma;
SERVICEB=zmc;
CURRENT_DATE=$(date ‘+%d/%m/%Y-%H:%M:%S’)

if
ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo “$CURRENT_DATE : $SERVICE service running, everything is fine” >> /var/log/zoneminderRun.log
else
echo “$CURRENT_DATE : $SERVICE is not running!” >> /var/log/zoneminderRun.log
echo “$CURRENT_DATE : I am going to restart $SERIVCE” >> /var/log/zoneminderRun.log
service zoneminder restart
fi
sleep 5
if
<> ps ax | grep -v grep | grep $SERVICEB > /dev/null
then
echo “$CURRENT_DATE : $SERVICEB service running, everything is fine” >> /var/log/zoneminderRun.log
else
echo “$CURRENT_DATE : $SERVICEB is not running!” >> /var/log/zoneminderRun.log
echo “$CURRENT_DATE : I am going to restart $SERIVCEB” >> /var/log/zoneminderRun.log
service zoneminder restart
fi