PHP script automatically monitors Nginx 504 errors

Source: Internet
Author: User

  1. #!/usr/bin/php

  2. <?php

  3. Error_reporting (E_error);

  4. Define ("email", "[email protected]");//Your email address

  5. Define ("Timeout", 5);//Read site time-out, 5 seconds

  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. function Check ($d, $k) {

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

  20. $html = Curl_get ($url);

  21. if (Substr_count ($html, $k) &lt; 1) {

  22. Mail (EMAIL, "JP 504 timeout for". $d, $html);

  23. EXEC ("/etc/init.d/nginx restart"); Re-start Nginx

  24. EXEC ("/etc/init.d/php-fpm restart");//Restart PHP-FPM

  25. }

  26. }

  27. Check ("https://www.linuxidc.com", "linuxidc.com");

  28. ?>

Name the file 504check.php
Modify Permissions chmod +x 504check.php

Then crontab-e add a row

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

Every minute the system automatically detects if the website responds slowly, and if so, restarts.

This article from "Li Shilong" blog, declined reprint!

PHP script automatically monitors Nginx 504 errors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.