我不是 PHP 专家。我有一个函数,它使用 EXEC 运行 WINRS,然后在远程服务器上运行命令。问题是这个函数被放置在一个循环中,该循环调用了几十次 getservicestatus 函数。有时 WINRS 命令可能会卡住或花费比预期更长的时间,从而导致 PHP 脚本超时并引发 500 错误。
我暂时降低了 PHP 中的设置超时值,并在 IIS 中创建了一个自定义 500 页面,如果引用页面等于脚本名称,则重新加载页面(否则,抛出错误)。但这很混乱。显然,它并不适用于每次调用该函数,因为它是全局的。所以它只会避免页面在 HTTP 500 错误处停止。
我真正想做的是在函数本身上设置 5 秒的超时。我一直在搜索,但即使在 stackoverflow 上也找不到答案。是的,有类似的问题,但我找不到任何与我的职能相关的问题。也许在执行命令时有一种方法可以做到这一点,例如 exec() 的替代方法?我不知道。理想情况下,我希望函数在 5 秒后超时并将 $servicestate 返回为 0。
代码被注释以解释我的意大利面条混乱。我很抱歉你必须看到它......
function getservicestatus($servername, $servicename, $username, $password)
{
//define start so that if an invalid result is reached the function can be restarted using goto.
start:
//Define command to use to get service status.
$command = 'winrs /r:' . $servername . ' /u:' . $username . ' /p:' . $password . ' sc query ' . $servicename . ' 2>&1';
exec($command, $output);
//Defines the server status as $servicestate which is stored in the fourth part of the command array.
//Then the string "STATE" and any number is stripped from $servicestate. This will leave only the status of the service (e.g. RUNNING or STOPPED).
$servicestate = $output[3];
$strremove = array('/STATE/','/:/','/[0-9]+/','/\s+/');
$servicestate = preg_replace($strremove, '', $servicestate);
//Define an invalid output. Sometimes the array is invalid. Catch this issue and restart the function for valid output.
//Typically this can be caught when the string "SERVICE_NAME" is found in $output[3].
$badservicestate = "SERVICE_NAME" . $servicename;
if($servicestate == $badservicestate) {
goto start;
}
//Service status (e.g. Running, Stopped Disabled) is returned as $servicestate.
return $servicestate;
}