如何使用Process类

时间:2014-05-22 17:23:55

标签: php

如何使用以下类启动流程?

<?php
/* An easy way to keep in track of external processes.
 * Ever wanted to execute a process in php, but you still wanted to have somewhat controll of the process ? Well.. This is a way of doing it.
 * @compability: Linux only. (Windows does not work).
 * @author: Peec
 */
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;
    }
}

我想要执行的命令是:

cd /home/bot/bot-victory/ && nohup ./ghost++

我如何使用课程开始,停止和获取我的过程的状态。

1 个答案:

答案 0 :(得分:1)

看起来你可以这样做:

$process = new Process('/home/bot/bot-victory/ghost++');

这也将启动该过程。注意,我稍微修改了你的命令,但它应该以相同的方式工作(你链接的Process类已经将nohup添加到命令中)。

停止:

$process->stop();

状态:

$process->status();