#!/usr/bin/php
<?php
error_reporting(E_ERROR);
define("EMAIL","lishilongk@163.com.com");//你的email地址
define("TIMEOUT",5);//读取网站超时时间,5秒
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 "); //重启nginx
exec("/etc/init.d/php-fpm restart");//重启php-fpm
}
}
check("https://www.linuxidc.com","linuxidc.com");
?>
将该文件命名为 504check.php
修改权限 chmod +x 504check.php
然后crontab -e添加一行
* * * * * /xx/504check.php >/dev/null 2>&1
每分钟系统就会自动检测网站是否响应很慢,若如此,则重启。