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.
posix_kill
(PHP 4, PHP 5)
posix_kill — プロセスにシグナルを送信する
説明
bool posix_kill
( int $pid
, int $sig
)
シグナル sig をプロセス ID pid のプロセスに送信します。
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
参考
- POSIX システムの kill(2) マニュアルページも参照ください。 そこには、負のプロセスID、特別プロセスID 0、特別プロセスID -1、 シグナル番号 0 に関する追加情報があります。
posix_kill
Jille at mydevnull dot quis dot cx
16-Apr-2008 10:22
16-Apr-2008 10:22
Jacques Manukyan
22-Mar-2008 11:40
22-Mar-2008 11: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
03-Feb-2005 01:42
03-Feb-2005 01: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 09:26
18-Dec-2002 09: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";
}
}