我在家里组装并配置了一台迷你服务器,运行带有 Apache 的 Debian GNU/Linux。我还安装了开源视频游戏Teeworlds为了运行专用服务器。
我的想法是使用 Apache 和 PHP 在我的网站上显示一些有关服务器状态的信息。最重要的功能是显示服务器是否正在运行。
我首先尝试使用 pgrep 命令,该命令适用于许多进程,但不适用于我的情况:
<?php exec("pgrep process_name", $output, $return);
if ($return == 0) {
/*Service is running*/
}
else{
/*Service is not running*/
}
?>
接下来我发现这在终端中有效:
ps -ef | awk '$8=="/usr/games/teeworlds-server" {print $2}'
但我不能将它与 PHP exec 命令一起使用,因为该字符串使用双引号(它不适用于单引号)。
还有其他方法可以使用 PHP 检查进程的状态吗?或者我做错了什么?
注意:该游戏已从官方 Debian 存储库安装。
最佳答案
我通过更改 exec 命令解决了这个问题:
exec("ps -ef | awk '$8==\"/usr/games/teeworlds-server\"{print $2}'", $output, $return)
使用shell_exec:
$test= shell_exec("ps -ef | awk '$8==\"/usr/games/teeworlds-server\" {print $2}'");
if ($test>0) {
/*Server is running*/
}
else{
/*Server is not running*/
}
关于PHP - 验证程序是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26253489/