函数检查服务器($domain,$port=80)
{
全局$checkTimeout,$testserver;
$status = 0;
$starttime = microtime(true);
$file = @fsockopen ($domain, $port, $errno, $errstr, $checkTimeout);
$stoptime = microtime(true);
if($file)
{
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
else
{
$testfile = @fsockopen ($testServer, 80, $errno, $errstr, $checkTimeout);
if($testfile)
{
fclose($testfile);
$status = -1;
}
else
{
$status = -2;
}
}
return $status;
}
测试服务器是google.sk,checkTimeout是10秒。这实际上是可行的,但是当我尝试在一个循环中运行它大约50次,并且做其他事情(MySQL查询和类似的事情)时,它并不慢,但是它会导致我的CPU 100%的负载,直到脚本结束。这是一个让我的CPU疯狂的Apache进程…所以我想问你对这件事有什么看法。也许一些关于如何在python或bash中执行相同操作的技巧会受到赞赏。
感谢您的回复:)