PHP 指令碼自動監控 Nginx 504錯誤

來源:互聯網
上載者:User

標籤:php   指令碼   自動監控   

  1. #!/usr/bin/php

  2. <?php

  3. error_reporting(E_ERROR);

  4. define("EMAIL","[email protected]");//你的email地址

  5. define("TIMEOUT",5);//讀取網站逾時時間,5秒

  6. function curl_get($url){

  7.                 $curl = curl_init();

  8.                 curl_setopt($curl, CURLOPT_URL, $url);

  9.                 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  10.                 curl_setopt($curl, CURLOPT_TIMEOUT, TIMEOUT);

  11.                 // Headers

  12.                 $headers = array();

  13.                 $headers[] = "Date: ".date(‘r‘);

  14.                 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

  15.                 $Data = curl_exec($curl);

  16.                 return $Data ;

  17.  

  18. }

  19.  

  20. function check($d,$k){

  21.         $url = $d;//"http://".$d;

  22.         $html = curl_get($url);

  23.         if( substr_count($html,$k) &lt; 1){

  24.                 mail(EMAIL,"JP 504 timeout for ".$d,$html);

  25.                 exec("/etc/init.d/nginx restart "); //重啟nginx

  26.                 exec("/etc/init.d/php-fpm restart");//重啟php-fpm

  27.         }

  28. }

  29.  

  30. check("https://www.linuxidc.com","linuxidc.com");

  31. ?>

將該檔案命名為 504check.php
修改許可權 chmod +x 504check.php

然後crontab -e添加一行

* * * * * /xx/504check.php >/dev/null 2>&1

每分鐘系統就會自動檢測網站是否響應很慢,若如此,則重啟。

本文出自 “李世龍” 部落格,謝絕轉載!

PHP 指令碼自動監控 Nginx 504錯誤

聯繫我們

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