php中的多线程监控进程并行处理
我在php站点上发现了这个示例,我想运行一个命令并监视其运行的进程。 请在下面找到课程代码php中的多线程监控进程并行处理,php,parallel-processing,Php,Parallel Processing,我在php站点上发现了这个示例,我想运行一个命令并监视其运行的进程。 请在下面找到课程代码 class Process{ private $pid; private $command; public function __construct($cl=false){ if ($cl != false){ $this->command = $cl; $this->runCom(); } } private function runCom(
class Process{
private $pid;
private $command;
public function __construct($cl=false){
if ($cl != false){
$this->command = $cl;
$this->runCom();
}
}
private function runCom(){
$command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!';
exec($command ,$op);
$this->pid = (int)$op[0];
}
public function setPid($pid){
$this->pid = $pid;
}
public function getPid(){
return $this->pid;
}
public function status(){
$command = 'ps -p '.$this->pid;
exec($command,$op);
if (!isset($op[1]))return false;
else return true;
}
public function start(){
if ($this->command != '')$this->runCom();
else return true;
}
public function stop(){
$command = 'kill '.$this->pid;
exec($command);
if ($this->status() == false)return true;
else return false;
}
下面是我用来运行它的脚本
require 'class.php';
$process = new Process('ls -al');
// Then you can start/stop/ check status of the job.
$process.stop();
$process.start();
if ($process.status()){
echo "The process is currently running";
}else{
echo "The process is not running.";
}
我听到一个错误,说未定义函数start()请尝试:
$process->start();
这是正确的php语法。我收到一个错误,说我使用了未定义的函数start(),但它抛出的进程没有运行。但是在命令行中,我输入了ls-al,它可以工作。
ls-al
可能不是一个好的测试场景,因为它只运行几毫秒。尝试一些需要更多时间的命令。你能推荐一个我可以尝试的命令吗?我对linux命令不太熟悉,请执行du-hs/usr
。。。有创意