PHP: posix_kill - Manual

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

search for in the

posix_mkfifo posix_isatty

Last updated: Fri, 26 Jan 2007

view this page in

# posix_kill

(PHP 3 >= 3.0.13, PHP 4, PHP 5)

posix_kill -- Send a signal to a process

## Description

bool posix_kill ( int pid, int sig )

Send the signal `sig` to the process with the process identifier `pid`. Returns `FALSE`, if unable to send the signal, `TRUE` otherwise.

See also the kill(2) manual page of your POSIX system, which contains additional information about negative process identifiers, the special pid 0, the special pid -1, and the signal number 0.

add a note User Contributed Notes
posix_kill
codeslinger at compsalot dot com
02-Feb-2005 08: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
17-Dec-2002 04: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";
}
}`
add a note
posix_mkfifo posix_isatty

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST