監視系統服務mysqld、httpd的運行狀態
環境要求:
編寫指令碼程式(shell.sh)用於每隔5分監視一次系統服務mysqld的運行狀態,
若發現mysqld進程已經停止,則在/var/log/messages檔案中追加寫入日誌資訊,包括停止時間,並重啟mysqld服務;
否則不進行任何操作:
如下:
650) this.width=650;" title="image001.png" src="http://www.bkjia.com/uploads/allimg/131227/1K033G56-0.png" />
內容如下:
#!/bin/bash
pgrep -x mysqld &> /dev/null
if [ $? -ne 0 ]
then
echo "At time: `date` :MySQL server is down .">> /var/log/messages
servicemysqld start
else
echo "MySQL server is running ."
fi
添加相應許可權,執行指令碼驗證效果:
650) this.width=650;" title="image003.png" src="http://www.bkjia.com/uploads/allimg/131227/1K0334N3-1.png" />
設定相應的計劃任務
環境要求:
編寫指令碼程式用於監視系統服務httpd的運行狀態,要求如下
當服務狀態失常時在"/var/log/htmon.log"檔案中記入日誌資訊。
自動將狀態失常的httpd服務重新啟動。若重啟httpd服務失敗,測嘗試重新啟動伺服器主機
結合crond計劃任務,每周一至周五每隔15分鐘執行一次監測任務
1、在/opt目錄中建立指令碼htmon.sh,指令碼內容如下
如下:
650) this.width=650;" title="image005.png" src="http://www.bkjia.com/uploads/allimg/131227/1K0332O2-2.png" />
內容如下:
#! /bin/bash
/sbin/service httpd status &> /dev/null
if [ $? -ne 0 ]
then
echo "httpd is down. at time: `date`" >> /var/log/httpshell.log
/sbin/service httpd restart
/sbin/service httpd status &> /dev/null
if [ $? -ne 0 ]
then
/sbin/chkconfig --level 2345 httpd on
/sbin/shutdown -r now
fi
fi
2、建立crontab計劃任務,
*/15 * * * * /root/httpshell.sh
650) this.width=650;" title="image007.png" src="http://www.bkjia.com/uploads/allimg/131227/1K0333T9-3.png" />
3、執行指令碼並驗證效果:
650) this.width=650;" title="image009.png" src="http://www.bkjia.com/uploads/allimg/131227/1K033M46-4.png" />
[root@shell ~]# chmod +x httpshell.sh
[root@shell ~]# ./httpshell.sh
[root@shell ~]# tail -5 /var/log/httpshell.log
本文出自 “聽聞” 部落格,請務必保留此出處http://wenzhongxiang.blog.51cto.com/6370734/1229558