note 96744 deleted from function.proc-terminate by danbrown
| From: | danbrown@php.net | Date: | Mon, 15 Mar 2010 16:26:30 +0000 |
| Subject: | note 96744 deleted from function.proc-terminate by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-167047@lists.php.net to get a copy of this message | ||
Note Submitter: mast at imast.ru
----
Method described in the previous note will not work if your child process has his own childs.
There is easier way to solve the problem with non-finishing child processes:
<?php
$descriptorspec = array(
0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
2 => array('pipe', 'w') // stderr is a pipe the child will write to
);
$proc = proc_open('bad_program', $descriptorspec, $pipes);
if(!is_resource($proc)) {
throw new Exception('bad_program could not be started.');
}
// Get current PID
$status = proc_get_status($proc);
$pid = $status['pid'];
// Set process group for the bunch of processes (for easy killing)
posix_setpgid($pid, $pid);
// Some actions
// It's time to kill
// Close pipes
fclose($pipe[0]);
fclose($pipe[1]);
fclose($pipe[2]);
// Send SIGKILL to all processes within group
posix_kill(-$pid, 9);
// Close original process
proc_close($proc);
?>