This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Added kill method to Proc.

  • Loading branch information...
1 parent ca51316 commit 6d91176365339649f9d0c0e635ac893274c299bc @L-P committed Sep 23, 2011
Showing with 26 additions and 9 deletions.
  1. +2 −1 TODO
  2. +24 −8 proc.php
View
3 TODO
@@ -1 +1,2 @@
-1/ Find a way to write more than once to stdin.
+1/ Make out/err calls non-blocking.
+2/ Find a way to write more than once to stdin.
View
@@ -11,6 +11,9 @@ class Proc {
const STDOUT = 1; //< Index of the stdout pipe.
const STDERR = 2; //< Index of the stderr pipe.
+ const SIGTERM = 15; //< SIGTERM signal number.
+ const SIGKILL = 9; //< SIGKILL signal number.
+
protected $proc = null; //< Our proc handler.
protected $pipes = null; ///< Array with stdin, stdout and stderr.
@@ -76,14 +79,7 @@ public function close() {
throw new \RuntimeException('Process not running.');
// PHP says we must close pipes to avoid deadlocks when closing the process.
- foreach($this->pipes as $pipe) {
- if(empty($pipe))
- continue;
-
- fclose($pipe);
- }
- $this->pipes = null;
- $this->wroteToIn = false;
+ $this->closePipes();
$ret = proc_close($this->proc);
$this->proc = null;
@@ -129,6 +125,26 @@ public function in($in) {
$this->wroteToIn = true;
}
+ protected function closePipes() {
+ foreach($this->pipes as $pipe) {
+ if(empty($pipe))
+ continue;
+
+ fclose($pipe);
+ }
+ $this->pipes = null;
+ $this->wroteToIn = false;
+ }
+
+ /// Kills the process.
+ public function kill($signal = self::SIGTERM) {
+ if(!$this->proc)
+ throw new \RuntimeException('Process not running.');
+
+ $this->closePipes();
+ proc_terminate($this->proc, $signal);
+ $this->proc = null;
+ }
/// Destructor.
public function __destruct() {

0 comments on commit 6d91176

Please sign in to comment.