Program execution 函数
在线手册:中文  英文

proc_terminate

(PHP 5)

proc_terminateKills a process opened by proc_open

说明

bool proc_terminate ( resource $process [, int $signal = 15 ] )

Signals a process (created using proc_open()) that it should terminate. proc_terminate() returns immediately and does not wait for the process to terminate.

proc_terminate() allows you terminate the process and continue with other tasks. You may poll the process (to see if it has stopped yet) by using the proc_get_status() function.

参数

process

The proc_open() resource that will be closed.

signal

This optional parameter is only useful on POSIX operating systems; you may specify a signal to send to the process using the kill(2) system call. The default is SIGTERM.

返回值

Returns the termination status of the process that was run.

更新日志

版本 说明
5.2.2 Previous versions used to destroy the given process resource.

参见


Program execution 函数
在线手册:中文  英文

用户评论:

jerhee at ucsd dot edu (2008-02-23 20:08:10)

As explained in http://bugs.php.net/bug.php?id=39992, proc_terminate() leaves children of the child process running. In my application, these children often have infinite loops, so I need a sure way to kill processes created with proc_open(). When I call proc_terminate(), the /bin/sh process is killed, but the child with the infinite loop is left running. 

Until proc_terminate() gets fixed, I would not recommend using it. Instead, my solution is to:
1) call proc_get_status() to get the parent pid (ppid) of the process I want to kill. 
2) use ps to get all pids that have that ppid as their parent pid
3) use posix_kill() to send the SIGKILL (9) signal to each of those child pids
4) call proc_close() on process resource

<?php
$descriptorspec 
= array(
=> array('pipe''r'),  // stdin is a pipe that the child will read from
=> array('pipe''w'),  // stdout is a pipe that the child will write to
=> array('pipe''w')   // stderr is a pipe the child will write to
);
$process proc_open('bad_program'$descriptorspec$pipes);
if(!
is_resource($process)) {
    throw new 
Exception('bad_program could not be started.');
}
//pass some input to the program
fwrite($pipes[0], $lots_of_data);
//close stdin. By closing stdin, the program should exit
//after it finishes processing the input
fclose($pipes[0]);

//do some other stuff ... the process will probably still be running
//if we check on it right away

$status proc_get_status($process);
if(
$status['running'] == true) { //process ran too long, kill it
    //close all pipes that are still open
    
fclose($pipes[1]); //stdout
    
fclose($pipes[2]); //stderr
    //get the parent pid of the process we want to kill
    
$ppid $status['pid'];
    
//use ps to get all the children of this process, and kill them
    
$pids preg_split('/\s+/', `ps -o pid --no-heading --ppid $ppid`);
    foreach(
$pids as $pid) {
        if(
is_numeric($pid)) {
            echo 
"Killing $pid\n";
            
posix_kill($pid9); //9 is the SIGKILL signal
        
}
    }
        
    
proc_close($process);
}

?>

易百教程