快捷搜索:   nginx

自动检测postfix-pop3-mysql服务运行状态的脚本 失败自动重启

用于自动检测服务器上的相关服务运行状态,如果服务未运行,或者运行失败自动发邮件提醒,并试图重启。
保存为.sh并设置chmod +x
#!/bin/bash
#Purpose: Check Postfix and Pop3 MySQL service status
#Author: andylhz
#Date: 2010-07-29

dt=`date '+%Y:%m:%d:%H:%M'`
mailer=root@bnxb.com
mailer2=138XXXXXXXX@139.com
host=mail.bnxb.com
#Dovecot服务的检查 存在则发邮件显示正常,否则就重启服务

dovecot=`ps aux |grep pop3-login | grep -v "grep" |awk {'print $11'} | uniq`
if [ "$dovecot" == "pop3-login" ]; then
       echo "$dt <$host> pop3 service is ok!" >/tmp/pop_ok;
       mail -s "pop3-OK(IP-154)"  $mailer < /tmp/pop_ok;
   else
       echo "$dt  <$host> pop3 service is down! will start!" >/tmp/pop_down;
       mail -s  "pop3-down(IP-)" -c $mailer2  $mailer < /tmp/pop_down;
       service  dovecot start;
fi
#postfix服务的检查 存在则发邮件显示正常,否则就重启服务

postfix=`ps aux |grep postfix | grep -v "grep" |grep -v "root"| awk {'print $1'} | uniq`
if [ "$postfix" == "postfix" ]; then
        echo "$dt <$host> postfix service is ok!" >/tmp/postfix_ok;
        mail  -s "postfix-OK(IP)"  $mailer < /tmp/postfix_ok;
   else
       echo "$dt <$host> postfix service is down! will start!">/tmp/postfix_down;
       mail -s "postfix-down(IP)" -c $mailer2 $mailer < /tmp/postfix_down;
       service  postfix start;
fi
#Mysql 的状态检查,看是否存在mysql?存在则发邮件报告正常,否则就重启服务

mysql=`ps aux |grep mysql |grep -v "grep" |awk {'print $1'} |grep mysql`
if [ "$mysql" == "mysql" ]; then
        echo "$dt <$host> mysql is ok! " >/tmp/mysql_ok; 
        mail -s "mysql-OK(IP)" $mailer < /tmp/mysql_ok;

  else

       echo "$dt <$host> mysql is down! will start!" > /tmp/mysql_down;
       mail -s "mysql-down(IP)" -c $mailer2 $mailer < /tmp/mysql_down;
        service  mysqld start;
fi

将上述保存成可执行的文件,添加cron计划任务中,每10分钟运行一次


顶(5)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论