如何添加sersync进程监控脚本
sersync監控腳本不但能對相關進程進行監控,還能夠自動將停止的進程自動重啟。下面就直接講解一下sersync監控ing腳本的具體操作方法。
環境介紹
sersync安裝在/root/shell/下
發送報警的郵件正文在/data/log/sersync/bady.txt
sersync 的日志位置放置在 /data/log/sersync/checksersync.log中
腳本會自動將最新的日志內容添加在郵件正文中
以上路徑有操作者自己建立
腳本內容如下
#!/bin/bash
# usage:
#?????? 1.將腳本命名為checkrsync.sh 放到/root/shell
#?????? 2 chmod +x checkrsync.sh 給腳本賦予可執行權限
#?????? 2.添加到計劃任務
#?????? */1 * * * * /root/shell/checkrsync.sh
logfile='/data/log/sersync/checksersync.log';
function sersync_is_running(){
??????? threadnum=`ps aux|grep sersync2|grep -v grep -wc`;????? #此處為監控sersync的監控手段,查找sersync2進程
??????? if [ "$threadnum" -eq '0' ];then
??????????????? echo '0';
??????? else
??????????????? echo '1';
??????? fi
??????? return;
}
function current_time(){
??????? if [ -z "$1" ];then
??????????????? format="%Y-%m-%d %H:%M:%S%Z";
??????? else
??????????????? format=$1;
??????? fi
??????? echo `date +"$format"`;
??????? return;
}
function logtofile(){
??????? echo $(current_time) $2>>$1;
}
function sersync_restart(){
??????? /root/shell/sersync2 -r -d -o /root/shell/confxml.xml >/dev/null 2>&1;
??????? sleep 3;
??????? threadnum=$(sersync_is_running);
??????? if [ $threadnum -eq '0' ]; then
??????????????? echo "0";
??????? else
??????????????? echo '1';
??????? fi
??????? return;
}
isrunning=$(sersync_is_running);
mailzt=`cat $logfile|tail -1 > /data/log/sersync/body.txt`
if [ "$isrunning" -eq '0' ];then
??????? logtofile $logfile "mail sersync service was died.";
??????? mail -s "test sersync service was died." sms@126.com < /data/log/sersync/body.txt
??????? restart=$(sersync_restart);
??????? if [ $restart -eq '0' ];then
??????????????? logtofile $logfile "sersync service restart failed.";
??????? mail -s "test sersync service was died." sms@126.com < /data/log/sersync/body.txt
??????? else
??????????????? logtofile $logfile "sersync service restart success.";
??????? mail -s "test sersync service restart success" sms@126.com < /data/log/sersync/body.txt
??????? fi
else
??????? logtofile $logfile "sersync service is running.";
fi
exit 0;
?
?
test為安裝了sersync的主機
sms@126.com為發送報警的郵箱
為保證能夠發送報警郵件別忘了把sendmail的服務打開
service? sendmail? start
轉載于:https://blog.51cto.com/yjw1983/412909
總結
以上是生活随笔為你收集整理的如何添加sersync进程监控脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计原则笔记
- 下一篇: MySQL存储过程之代码块、条件控制、迭