douhuan1979 2014-10-08 09:26 采纳率: 100%
浏览 68
已采纳

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

I assembled and configured a mini server at home running Debian GNU/Linux with Apache. I also installed the open source video game Teeworlds in order to run a dedicated server.

My idea is to display some information regarding the status of the server on my website, using Apache and PHP. The most important feature is to display if the server is running or not.

I tried using the pgrep command at first that works fine with many processes but not in my case:

<?php exec("pgrep process_name", $output, $return);
  if ($return == 0) {
    /*Service is running*/
  }
  else{
    /*Service is not running*/
  }
?>

Next I found out that this works in the terminal:

ps -ef | awk '$8=="/usr/games/teeworlds-server" {print $2}'

but I can't use it with the PHP exec command because this string uses double quotes (it doesn't work this single quotes).

Is there any other way to check the status of a process using PHP? Or am I doing something wrong?

Note: The game has been installed from the official Debian repositories.

  • 写回答

2条回答 默认 最新

  • dreamact3026 2014-10-08 10:00
    关注

    I solved this problem by changing the exec command:

    exec("ps -ef | awk '$8==\"/usr/games/teeworlds-server\" {print $2}'", $output, $return)

    with 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*/
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?