Simple monitoring script using pgrep command

By | September 21, 2009

This is a usefull script to monitor the service status.

The script checks the service status using pgrep command. If the command return zero script identifies the service down and send us an alert email.

#!/bin/bash apache=`pgrep httpd` mysql=`pgrep mysqld` tmp="/tmp/monitoring.tmp" touch /tmp/monitoring.tmp if [ -n "$apache" ] then echo "Apache Is Working" else echo "Apache Service Down" >> $tmp fi if [ -n "$mysql" ] then echo "Mysqld Is Working" else echo "Mysqld Service Down" >> $tmp fi EMAIL="****@talk2melbin.com" SUBJECT="Alert Service Down" MESSAGE="$tmp" COUNT=`wc -m $tmp | awk '{print $1}'` echo "$COUNT" if [ $COUNT -ne 0 ]; then /bin/mail -s "$SUBJECT" "$EMAIL" < $MESSAGE fi rm -rf /tmp/monitoring

Cheers!
Melbin Mathew
www.talk2melbin.com

Melbin Mathew

I am Melbin Mathew, Systems Engineer from Kerala, India. I live with my parents, daughter and my wonderful wife. I graduated in 2004 with a bachelor’s degree in Electronics and Hardware from Mahatma Gandhi University, Kottayam and completed certification in MCITP, RHCE, CCNA, VCP ...Read More
Category: Scripts

About Melbin Mathew

I am Melbin Mathew, Systems Engineer from Kerala, India. I live with my parents, daughter and my wonderful wife. I graduated in 2004 with a bachelor’s degree in Electronics and Hardware from Mahatma Gandhi University, Kottayam and completed certification in MCITP, RHCE, CCNA, VCP ...Read More

  • Ramesh

    Hi Melbin,

    Using this script, I am getting error this file

    if [ $COUNT -ne 0 ]; —-> command not found error.

    Once the httpd down the mail automatically sent to admin.

    Please help me

    Thanks for advance

    • http://www.talk2melbin.com Melbin Mathew

      Hi Ramesh,

      Thank you for the feedback.

      This bash script is meant for Redhat varient distributions. Let me know more details. Which distrubution are you using? From where you are executing the script? Please paste the output of the script using “bash -x test.sh”.

      Thanks,

      Melbin Mathew