downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

shell_exec> <proc_open
Last updated: Fri, 12 Mar 2010

view this page in

proc_terminate

(PHP 5)

proc_terminate Termine un processus ouvert par proc_open()

Description

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

proc_terminate() envoie un signal au processus process (créé avec proc_open()) pour lui indiquer qu'il doit se terminer. proc_terminate() se termine immédiatement après, et n'attend pas l'arrêt réel du processus.

proc_terminate() vous permet de conclure un processus, et de continuer les autres tâches. Vous pouvez tester la présence de votre processus en utilisant la fonction proc_get_status().

Liste de paramètres

process

La ressource proc_open() qui sera fermée.

signal

Ce paramètre optionnel n'est utile que sur les plates-formes POSIX : vous pouvez alors spécifier un signal à envoyer au processus, en utilisant l'appel système kill(2). La valeur par défaut est alors SIGTERM.

Valeurs de retour

Retourne le code de sortie du processus.

Historique

Version Description
5.2.2 Les versions précédentes détruisent la ressource resource fournie.

Voir aussi

  • proc_open() - Exécute une commande et ouvre les pointeurs de fichiers pour les entrées / sorties
  • proc_close() - Ferme un processus ouvert par proc_open
  • proc_get_status() - Lit les informations concernant un processus ouvert par proc_open



shell_exec> <proc_open
Last updated: Fri, 12 Mar 2010
 
add a note add a note User Contributed Notes
proc_terminate
mast at imast.ru
13-Mar-2010 08:29
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);

?>
mast at imast.ru
13-Mar-2010 08:28
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);

?>
jerhee at ucsd dot edu
24-Feb-2008 05:08
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(
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
);
$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($pid, 9); //9 is the SIGKILL signal
       
}
    }
       
   
proc_close($process);
}

?>

shell_exec> <proc_open
Last updated: Fri, 12 Mar 2010
 
 
show source | credits | sitemap | contact | advertising | mirror sites