0

我在 php 站点上找到了这个示例,我想运行一个命令并监视它运行的进程。请在课程代码下方找到。

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()

4

1 回答 1

0

尝试:

$process->start();

这是正确的 php 语法。

于 2012-11-30T10:27:46.833 回答