Monday, September 10, 2012

Service check using Bash script, sendmail with Google Account, and cron

Bash script
#!/bin/sh
SERVICE='jobs:work'

if [ `ps ax | grep -v grep | grep $SERVICE | wc -l` = 1 ]
then
    echo "$SERVICE service running, everything is fine"
else
    (
      echo "To: alert@abc.com"
      echo "Subject: $SERVICE is down. "
    ) | sendmail -t
fi
Install sendmail using Google Account on EC2
sudo su
apt-get update && apt-get install ssmtp
vi /etc/ssmtp/ssmtp.conf

AuthUser=abc@abc.com
AuthPass=abcabc
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
Install cron job for checking every 5 minutes
*/5 * * * * ~/jobs_check.sh

No comments: