UNIX Tutorials, Tips, Tricks and Shell Scripts

ProcMonUX - lfl_py_proc_mon

(See ProcMonUX - a Simple Lightweight Linux Process Monitor Script with Alerts, Restart and Logging for details about this script.)


IMPORTANT: BEFORE you deploy a script (or any new program, patch, application, database, upgrade, etc for that matter) in a production environment, always test in a non-production environment (i.e., development or QA) to ensure the script behaves as expected. This will keep your constituents happy and will enable you to make it home in time for dinner.  =)

PS=/bin/ps
GREP=/bin/grep
AWK=/bin/awk
LOG=/root/lfl/bin/logs/lfl_py_cb_chk.log
MAIL_RECIP="your_email@your_domain.com"

PROC_CNT=2

PROC_NAME[1]="mysqlquotad"
PROC_ACTION[1]="restart"
PROC_CMD[1]="nohup /usr/bin/perl /usr/local/bin/mysqlquotad /etc/mysql_quota.conf &"

PROC_NAME[2]="nginx"
PROC_ACTION[2]="email"
PROC_CMD[2]="/etc/init.d/nginx start"

i=0
while [ $i -ne $PROC_CNT ]
do
  (( i=i+1 ))
  #echo "${PROC_NAME[$i]}"
  $PS -ef | $GREP ${PROC_NAME[$i]} | $GREP -vq grep
  FOUND=$?
  if [ $FOUND -ne 0 ]
  then
    DATE=$(date)
    echo "${DATE}: ${PROC_NAME[$i]} not found!" >> $LOG
    if [ ${PROC_ACTION[$i]} == "restart" ]
    then
      DATE=$(date)
      mail -s "LFL/PROC/MONITORING: process ${PROC_NAME[$i]} was not found! Attempting to restart..." $MAIL_RECIP < /dev/null
      echo "${DATE}: attempting to restart ${PROC_NAME[$i]} with ${PROC_CMD[$i]} ..." >> $LOG
      su - root -c "${PROC_CMD[$i]}"
    elif [ ${PROC_ACTION[$i]} == "email" ]
    then
      echo "${DATE}: sending email notification to ${MAIL_RECIP} ..." >> $LOG
      mail -s "LFL/PROC/MONITORING: process ${PROC_NAME[$i]} was not found! MANUAL INTERACTION REQ'D!!!" $MAIL_RECIP < /dev/null
    fi
  fi
done

exit