我制作了ping脚本网站的PHP脚本,如果我有答案我什么也不做,否则我发送电子邮件提醒。这是我的代码:
function urlExists($url=NULL)
{
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300){
return true;
} else {
return false;
}
}
$url = 'http://www.website.com/';
if(urlExists($url) == true)
{
exit('Website OK'."\n");
} else {
$headers ='From: "Sembot"'."\n";
$headers .='Reply-To: noreply@website.com'."\n";
$headers .='Content-Type: text/html; charset="iso-8859-1"'."\n";
$headers .='Content-Transfer-Encoding: 8bit';
foreach ($destinataire as $dest) {
echo 'Website DOWN'."\n";
if(mail($dest, 'Sembot - Website DOWN', 'Website DOWN', $headers)) { echo 'email...'; }
}
}
当我在浏览器或控制台上执行此文件时,消息为“网站正常”,因为网站现在正常。但是当我创建一个每5分钟执行一次脚本的crontab时,我每5分钟收到一封电子邮件,但网站没有死...你知道为什么吗?