简体   繁体   中英

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. The Process is active in the Linux server is 16269 but its showing the "Process is dead". 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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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