简体   繁体   English

PHP脚本检查Linux进程是否处于活动状态

[英]PHP Script to check a Linux process is active

I just created a script to check a process is active or not below is my PHP script. 我刚刚创建了一个脚本来检查进程是否处于活动状态,以下是我的PHP脚本。 The Process is active in the Linux server is 16269 but its showing the "Process is dead". 在Linux服务器上活动的进程为16269,但显示“进程已死”。 Is there any way to check through a process name ? 有什么方法可以检查进程名称吗?

<?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>

This is the URL : http://fanciedmedia.in/proview/index.php 这是URL: http : //fanciedmedia.in/proview/index.php

Try the following : 尝试以下方法:

<?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>

It works fine on my end. 对我来说效果很好。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM