PHP - 验证程序是否正在运行

标签 php linux process debian open-source

我在家里组装并配置了一台迷你服务器,运行带有 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/

相关文章:

java - 如何查找 Java 堆大小和使用的内存 (Linux)?

git - 哪些信号可以安全地用于终止 Git 进程,哪些不能?

php - 错误异常 : Array to string conversion in PHP

php - 发布日期格式并从数据库中选择日期格式

linux - FEC 和 Gianfar 派生器

linux - ttyACM0 USB ACM 设备的不需要的重复项

node.js - 如何在退出时执行异步操作

C# 进程终止通知

php - 来自 SQL 正确 sql 查询的 IP 会计显示

c# - Unity 使用带有输入字段的脚本