#!/bin/bash #Scott Sullivan #Automated nginx service check/restart script. DATE=`date "+%m%d%y [%k:%M]"` LOGDATE=`date "+%m-%d-%y [%k:%M:%S]"` mail=/bin/mail sysadmin=ssullivan@liquidweb.com nginxfile=/tmp/nginx.txt logfile=/var/log/checknginx.log file=/var/log/checknginx.log if [ -e $file ]; then echo "" else touch /var/log/checknginx.log fi echo "$LOGDATE - Nginx Service Check starting up " >> $logfile function stopnginx { /etc/init.d/nginx stop > /dev/null 2>&1 killall -9 nginx > /dev/null 2>&1 } function startnginx { DATE=`date "+%m%d%y [%k:%M]"` LOGDATE=`date "+%m-%d-%y [%k:%M:%S]"` stopnginx echo "Nginx is down, attempting to restart on $DATE" >> $nginxfile cat $nginxfile | $mail -s 'Nginx Restart' $sysadmin /etc/init.d/nginx stop > /dev/null 2>&1 killall -9 nginx > /dev/null 2>&1 /etc/init.d/nginx start > /dev/null 2>&1 rm $nginxfile } function checkpid { DATE=`date "+%m%d%y [%k:%M]"` LOGDATE=`date "+%m-%d-%y [%k:%M:%S]"` compare=`ps aux | grep -v grep | grep "nginx: worker process"` if [ -z "$compare" ] then echo "$LOGDATE - No Nginx Running! Restarting..." >> $logfile startnginx else echo "$LOGDATE - Nginx Running..." >> $logfile fi } count="0" while [ $count -lt "10" ]; do checkpid sleep 60s done