(This is for linux users only).
We know now how we can fork a process in linux with the & operator.
And by using command: nohup MY_COMMAND > /dev/null 2>&1 & echo $! we can return the pid of the process.
This small class is made so you can keep in track of your created processes ( meaning start/stop/status ).
You may use it to start a process or join an exisiting PID process.
if ($process.status()){
echo"The process is currently running";
}else{
echo"The process is not running.";
}?>
* 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
*/classProcess{
private$pid;
private$command;
public function__construct($cl=false){
if ($cl!=false){$this->command=$cl;$this->runCom();
}
}
private functionrunCom(){$command='nohup '.$this->command.' > /dev/null 2>&1 & echo $!';exec($command,$op);$this->pid= (int)$op[0];
}
public functionsetPid($pid){$this->pid=$pid;
}
public functiongetPid(){
return$this->pid;
}
public functionstatus(){$command='ps -p '.$this->pid;exec($command,$op);
if (!isset($op[1]))returnfalse;
else returntrue;
}
public functionstart(){
if ($this->command!='')$this->runCom();
else returntrue;
}
public functionstop(){$command='kill '.$this->pid;exec($command);
if ($this->status() ==false)returntrue;
else returnfalse;
}
}?>