How to check if there is a there is a wget instance running

后端 未结 1 909
小鲜肉
小鲜肉 2021-01-23 10:21

I have this php script that will run wget\'s fork processes each time this is called with the & :

wget http://myurl?id=\'.$insert_id .\' -O          


        
相关标签:
1条回答
  • 2021-01-23 11:21

    This code is used to control running process (which in my case is php script).

    Feel free to take out parts that you need and use them as you please.

    class Process
    {
      private $processName;
      private $pid;
    
      public $lastMsg;
    
      public function __construct($proc)
      { 
        $this->processName = $proc;
        $this->pid = 0;
        $this->lastMsg = "";
      }
    
      private function update()
      { 
        $output = array();
        $cmd = "ps aux | grep '$this->processName' | grep -v 'grep' | awk '{ print $2; }' | head -n 1";
        exec($cmd, $output, $rv);
    
        if ($rv == 0 && isset($output[0]) && $output[0] != "")
          $this->pid = $output[0];
        else
          $this->pid = false;
    
        return;
      }
    
      public function start()
      { 
        // if process isn't already running,
        if ( !$this->is_running() )
        { 
          // call exec to start php script
          $op = shell_exec("php $this->processName &> /dev/null & echo $!");
    
          // update pid
          $this->pid = $op;
          return $this->pid;
        }
        else
        { 
          $this->lastMsg = "$this->processName already running";
          return false;
        }
      }
      public function is_running()
      {
        $this->update();
    
        // if there is no process running
        if ($this->pid === false)
        {
          $this->lastMsg = "$this->processName is not running";
          return false;
        }
        else
        {
          $this->lastMsg = "$this->processName is running.";
          return true;
        }
      }
    
      public function stop()
      {
        $this->update();
    
        if ($this->pid === false)
        {
          return "not running";
        }
        else
        {
          exec('kill ' . $this->pid, $output, $exitCode);
          if ($exitCode > 0)
            return "cannot kill";
          else
            return true;
        }
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题