監視系統服務mysqld、httpd的運行狀態

來源:互聯網
上載者:User

監視系統服務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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.