06.19 php saved
the_undefined
Tags add more
 
Note
the_undefined did not leave a note
  1. <?php
  2. class Process {
  3.     public $cmd;
  4.     protected $_process;
  5.     protected $_pipes;
  6.  
  7.     protected $_exitCode = false;
  8.     protected $_stdOut = false;
  9.     protected $_stdErr = false;
  10.  
  11.     public function start($cmd = null) {
  12.         if ($cmd) {
  13.             $this->cmd = $cmd;
  14.         }
  15.  
  16.         $cmd = '(' . $this->cmd . ') 3>/dev/null ; echo $? >&3';
  17.         $this->_process = proc_open($cmd, array(
  18.             0 => array('pipe', 'r'),
  19.             1 => array('pipe', 'w'),
  20.             2 => array('pipe', 'w'),
  21.             3 => array('pipe', 'w'),
  22.         ), $this->_pipes);
  23.     }
  24.  
  25.     public function running() {
  26.         $status = $this->_status();
  27.         return $status['running'];
  28.     }
  29.  
  30.     public function pid() {
  31.         $status = $this->_status();
  32.         return $status['pid'];
  33.     }
  34.  
  35.     public function close() {
  36.         $exitCode = -1;
  37.         if (is_resource($this->_pipes[3])) {
  38.             $exitCode = $this->_pipes[3] = (int)trim(stream_get_contents($this->_pipes[3]));
  39.  
  40.             // Get the stdout and stderr as well
  41.             $this->_pipes[1] = $this->_stdOut = trim(stream_get_contents($this->_pipes[1]));
  42.             $this->_pipes[2] = $this->_stdErr = trim(stream_get_contents($this->_pipes[2]));
  43.         }
  44.  
  45.         foreach ($this->_pipes as $pipe) {
  46.             if (is_resource($pipe)) {
  47.                 fclose($pipe);
  48.             }
  49.         }
  50.         if ($this->running()) {
  51.             proc_close($this->_process);
  52.         }
  53.         return $this->_exitCode = $exitCode;
  54.     }
  55.  
  56.     protected function _status() {
  57.         $status = proc_get_status($this->_process);
  58.         return $status;
  59.     }
  60.  
  61.     public function exitCode() {
  62.         return $this->_exitCode;
  63.     }
  64. }
  65. ?>
Parsed in 0.077 seconds, using GeSHi 1.0.7.14

Modify this Paste