用于检查 Linux 进程是否处于事件状态的 PHP 脚本

标签 php linux

我刚刚创建了一个脚本来检查进程是否处于事件状态,下面是我的 PHP 脚本。 Linux 服务器中的事件进程为 16269,但显示“进程已死亡”。有什么方法可以检查进程名称吗?

<?php

  $pid = $_POST["pid"] ;

  function checkPid($pid)
    {
     // create our system command
     $cmd = "ps $pid";

     // run the system command and assign output to a variable ($output)
     exec($cmd, $output, $result);

     // check the number of lines that were returned
     if(count($output) >= 2){

          // the process is still alive
          echo '<h1> Process is running </h1>';
          echo '<img src="/proview/green.gif" width="16" height="16" alt="Process is Alive" />';
          return true;
     }

     // the process is dead
     echo '<h1> Process is dead </h1>';
     echo '<img src="/proview/red.gif" width="16" height="16" alt="Process is dead"/>';
     return false;

}

if(isset($_POST['submit']))
{
   checkPid($pid);
} 
?>
<body>
<p>


<form method="post" action="index.php">
    <input type="text" name="pid">
    <input type="submit" value="Check Processe by ID" name="submit">  
</form>

</body>

这是网址:http://fanciedmedia.in/proview/index.php

最佳答案

尝试以下操作:

<?php

function checkPid($pid)
{

      // returns something like:
      // 8987 pts/0    00:00:00 bash
      $result =  exec("ps -A|grep {$pid}");

      if(preg_match("/{$pid}/",$result))
      {
         echo "PID {$pid} is running.";
         return true;
      }
      else
      {
         echo "PID {$pid} is NOT running.";
         return false;
      }

}

if( isset($_POST['submit']) && isset($_POST["pid"]) )
{
   checkPid( $_POST["pid"] );
} 
?>
<body>
<p>


<form method="post" action="index.php">
    <input type="text" name="pid">
    <input type="submit" value="Check Processe by ID" name="submit">  
</form>

</body>

对我来说效果很好。祝你好运!

关于用于检查 Linux 进程是否处于事件状态的 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31995894/

相关文章:

PHP 显示 MySQL 查询结果

php - WordPress 中某个类别中所有其他帖子的布局不同

linux - 只从 backports 编译一个特定的驱动程序?

linux - Fortran:如何获取集群的节点名称

linux - ls -LA 中的操作数是什么?

php - ElasticSearch 从 0.90 更改为 1.1

php - 操作码缓存实际上是如何工作的?

linux 后缀配置和主机名 FQDN

php - Codeigniter DB 查询链接先前调用中的错误数据

linux - 在 debian wheezy 上安装 ubuntu-extras