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

余生颓废 提交于 2019-12-04 05:20: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 ./images/'. $insert_id.' > /dev/null 2>&1 &

But how I can check if there is already a wget proces in progress and if there is one, don't run another one ?


回答1:


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;
    }
  }

}


来源:https://stackoverflow.com/questions/42512692/how-to-check-if-there-is-a-there-is-a-wget-instance-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!