我有这个php脚本,每次使用& 调用它时都会运行wget的fork进程:
wget http://myurl?id='.$insert_id .' -O ./images/'. $insert_id.' > /dev/null 2>&1 &
但我如何检查是否已经有wget进程正在进行中,如果有进程,请不要运行另一个进程?
答案 0 :(得分:0)
此代码用于控制运行进程(在我的情况下是php脚本)。
随意取出您需要的部件并随意使用。
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;
}
}
}