PHP指令碼監控Nginx 502異常並自動重啟php-fpm

來源:互聯網
上載者:User
PHP指令碼監控Nginx 502錯誤並自動重啟php-fpm

伺服器時不時出現Nginx 502 Bad Gateway,如果在電腦旁邊還好,要是半夜或者出去了,怎麼辦?

沒關係,寫個指令碼檢測服務狀態,發現異常,自動重啟。

自動重啟指令碼:

php $url = 'http://blog.rebill.info'; $cmd = '/usr/local/php/sbin/php-fpm restart';   for($i = 0; $i < 5; $i ++){         $exec = "curl  connect-timeout 3 -I $url 2>/dev/null";         $res = shell_exec($exec);           if(stripos($res, '502 Bad Gateway') !== false){                 shell_exec($cmd);                 exit();         } }

原理就是用curl擷取HTTP頭,發現502狀態代碼就執行重啟php-fpm的命令。

url和cmd根據實際情況改成自己的。然後放到crontab裡一分鐘執行一次。

*/1 * * * * /usr/bin/php /root/crontab/nginx502.php

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.