如何检查是否有正在运行的wget实例 [英] How to check if there is a there is a wget instance running

查看:199
本文介绍了如何检查是否有正在运行的wget实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个php脚本,每次使用& 进行调用时,该脚本都会运行wget的fork进程:

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 &

但是我如何检查是否有正在进行的wget处理,如果有,不要运行另一个?

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

推荐答案

此代码用于控制运行过程(在我的情况下是php脚本).

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

}

这篇关于如何检查是否有正在运行的wget实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆