php后台运行命令并管理后台运行程序
class ProcessModel{
private $pid;
private $command;
private $resultToFile = '';
public function __construct($cl=false){
if ($cl != false){
$this->command = $cl;
}
}
public function runCom($cl=false){
if($cl) {
$this->resultToFile = md5($cl);
$this->command = $cl;
$command = 'nohup '.$cl.' 1>/tmp/'.$this->resultToFile.'_http_proess.txt & echo $! 2>&1';
exec($command ,$op);
$this->pid = (int)$op[0];
return $this->pid;
}else{
return 0;
}
}
public function setPid($pid){
$this->pid = $pid;
}
public function getPid(){
return $this->pid;
}
public function getContent() {
$command = 'cat /tmp/'.md5($this->command).'_http_proess.txt';
exec($command ,$op);
if(is_array($op)) {
$op = implode("\n", $op);
}
return $op;
}
public function isRuning(){
$command = 'ps -p '.$this->pid;
exec($command,$op);
if (!isset($op[1])) {
return false;
}else{
return true;
}
}
public function stop(){
$command = 'kill '.$this->pid;
exec($command);
if ($this->status() == false){
return true;
}else{
return false;
}
}
}
转载至链接:https://my.oschina.net/colin86/blog/1859777