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

search for in the

posix_mkfifo> <posix_isatty
Last updated: Fri, 26 Dec 2008

view this page in

posix_kill

(PHP 4, PHP 5)

posix_killManda una señal a un proceso

Descripción

bool posix_kill ( int $pid , int $sig )

Manda la señal sig al proceso con el identificador de proceso pid . Devuelve FALSE, si no puede enviar la señal.Si sí la envia devuelve TRUE .

Vea también la página de manual kill(2) de su sistema POSIX, la cual contiene información adicional sobre los identificadores de proceso negativos, el pid especial 0, el pid especial -1, y la señal numero 0.



posix_mkfifo> <posix_isatty
Last updated: Fri, 26 Dec 2008
 
add a note add a note User Contributed Notes
posix_kill
regis dot fr dot php dot net at tornad dot net
24-Dec-2008 03:06
A little recursive function to kill a process and his childs.
it works fine for me and I don't have find something else to do it.
It's a mix of various scripts I've found.

<?php
   
function killProcessAndChilds($pid,$signal) {
       
exec("ps -ef| awk '\$3 == '$pid' { print  \$2 }'", $output, $ret);
        if(
$ret) return 'you need ps, grep, and awk';
        while(list(,
$t) = each($output)) {
            if (
$t != $pid ) {
               
killProcessAndChilds($t,$signal);
            }
        }
       
//echo "killing ".$pid."\n";
       
posix_kill($pid, 9);
    }
?>
Jille at mydevnull dot quis dot cx
16-Apr-2008 01:22
If you want to test whether processes owned by other users are running, you can use:

<?php
  $running
=posix_kill($pid, 0);
  if(
posix_get_last_error()==1) /* EPERM */
   
$running=true;
?>

If the process is owned by somebody else (and you're not root), you will get an EPERM.
On my system (FreeBSD) this is defined to 1.

You should test what the value of EPERM is on your system.
Jacques Manukyan
22-Mar-2008 02:40
Keep in mind that you can only send kill signals to processes owned by your UID.

If you are running your program as root, then you can send kill signals to all processes.
codeslinger at compsalot dot com
02-Feb-2005 04:42
Detecting if another copy of a program is running (*NIX specific)

One cute trick, to see if another process is running, is to send it signal 0.  Signal 0 does not actually get sent, but kill will check to see if it is possible to send the signal.  Note that this only works if you have permission to send a signal to that process.

A practical use for this technique is to avoid running multiple copies of the same program.  You save the PID to a file in the usual way...   Then during start-up you check the value of the PID file and see if that process currently exists.

This is not totally fool-proof.  In rare circumstances it is possible for an unrelated program to have the same recycled PID.  But that other program would most likely not accept signals from your program anyway (unless your program is root). 

To make it as reliable as possible, you would want your program to remove it's PID file during shutdown (see register_shutdown_function).  That way, only if your program crashed AND another program happened to use the same PID AND the other program was willing to accept signals from your program, would you get a wrong result.  This would be an exceedingly rare occurrence.  This also assumes that the PID file has not been tampered with (as do all programs that rely on PID files...). 

It's also possible to use 'ps x' to detect this, but using kill is much more efficient.

Here is the core routine:

    $PrevPid = file_get_contents($PathToPidFile);

    if(($PrevPid !== FALSE) && posix_kill(rtrim($PrevPid),0)) {
        echo "Error: Server is already running with PID: $PrevPid\n";
        exit(-99);
    } else {
        echo "Starting Server...";
    }

Hmmm...  if you want total 100% reliability, plus efficiency.  What you could do is to make the initial check using kill.  If it says not running, then you are ready to zoom.  But if kill says already running, then you could use:

//You can get the $ProgramName from $argv[0]
$Result = shell_exec('ps x | grep "' . $PrevPid . '" | grep "' . $ProgramName . '" | grep -v "grep"');

Assuming that your program has permissions to do this.  If you execute that and get back an empty string, then the other program is an imposter using a recycled PID and you are clear to go. 

-- Erik
gid at gifpaste dot net
18-Dec-2002 12:26
For those that want to kill everything matching a certain pattern (ala killall in for linux), try something like this.  Note that this is a good idea to do something like this for cross platform compatilibity, instead of executing killall, because killall for other UNIXes does just that, kills EVERYTHING.  :)

function killall($match) {
    if($match=='') return 'no pattern specified';
    $match = escapeshellarg($match);
    exec("ps x|grep $match|grep -v grep|awk '{print $1}'", $output, $ret);
    if($ret) return 'you need ps, grep, and awk installed for this to work';
    while(list(,$t) = each($output)) {
        if(preg_match('/^([0-9]+)/', $t, $r)) {
            system('kill '. $r[1], $k);
            if(!$k) $killed = 1;
        }
    }
    if($killed) {
        return '';
    } else {
        return "$match: no process killed";
    }
}

posix_mkfifo> <posix_isatty
Last updated: Fri, 26 Dec 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites