#!/usr/bin/php
<?php
Error_reporting (E_error);
Define ("email", "[email protected]");//Your email address
Define ("Timeout", 5);//Read site time-out, 5 seconds
function Curl_get ($url) {
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $url);
curl_setopt ($curl, Curlopt_returntransfer, 1);
curl_setopt ($curl, Curlopt_timeout, TIMEOUT);
Headers
$headers = Array ();
$headers [] = "Date:". Date (' R ');
curl_setopt ($curl, Curlopt_httpheader, $headers);
$Data = curl_exec ($curl);
return $Data;
}
function Check ($d, $k) {
$url = $d;//"http://". $d;
$html = Curl_get ($url);
if (Substr_count ($html, $k) < 1) {
Mail (EMAIL, "JP 504 timeout for". $d, $html);
EXEC ("/etc/init.d/nginx restart"); Re-start Nginx
EXEC ("/etc/init.d/php-fpm restart");//Restart PHP-FPM
}
}
Check ("https://www.linuxidc.com", "linuxidc.com");
?>
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