Skip to content

Instantly share code, notes, and snippets.

@freekrai
Last active November 9, 2016 04:34
Show Gist options
  • Save freekrai/eb072191041413496496 to your computer and use it in GitHub Desktop.
Save freekrai/eb072191041413496496 to your computer and use it in GitHub Desktop.
<?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).
*/
class Process{
private $pid;
private $command;
public function __construct($cl=false){
if ($cl != false){
$this->command = $cl;
$this->runCom();
}
}
private function runCom(){
$command = $this->command . '& 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;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment