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 add a note User Contributed Notes
php at tomward dot fmail dot co dot uk
27-Oct-2005 09:27
'kill -l' gives you a list of signals available on your UNIX. Eg. Redhat Linux:

 1) SIGHUP      2) SIGINT      3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS      8) SIGFPE
 9) SIGKILL    10) SIGUSR1    11) SIGSEGV    12) SIGUSR2
13) SIGPIPE    14) SIGALRM    15) SIGTERM    17) SIGCHLD
18) SIGCONT    19) SIGSTOP    20) SIGTSTP    21) SIGTTIN
22) SIGTTOU    23) SIGURG      24) SIGXCPU    25) SIGXFSZ
26) SIGVTALRM  27) SIGPROF    28) SIGWINCH    29) SIGIO
30) SIGPWR      31) SIGSYS      33) SIGRTMIN    34) SIGRTMIN+1
35) SIGRTMIN+2  36) SIGRTMIN+3  37) SIGRTMIN+4  38) SIGRTMIN+5
39) SIGRTMIN+6  40) SIGRTMIN+7  41) SIGRTMIN+8  42) SIGRTMIN+9
43) SIGRTMIN+10 44) SIGRTMIN+11 45) SIGRTMIN+12 46) SIGRTMIN+13
47) SIGRTMIN+14 48) SIGRTMIN+15 49) SIGRTMAX-15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

Mac OS X Tiger:

 1) SIGHUP      2) SIGINT      3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGEMT      8) SIGFPE
 9) SIGKILL    10) SIGBUS      11) SIGSEGV    12) SIGSYS
13) SIGPIPE    14) SIGALRM    15) SIGTERM    16) SIGURG
17) SIGSTOP    18) SIGTSTP    19) SIGCONT    20) SIGCHLD
21) SIGTTIN    22) SIGTTOU    23) SIGIO      24) SIGXCPU
25) SIGXFSZ    26) SIGVTALRM  27) SIGPROF    28) SIGWINCH
29) SIGINFO    30) SIGUSR1    31) SIGUSR2

For example, note that SIGUSR1 is variously 10 or 30...
codeslinger at compsalot dot com
03-Feb-2005 12: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
andrey at php dot net
01-Nov-2004 09:58
Always use constants. Numbers are not portable.
cosminb at as dot ro
02-Oct-2003 10:15
a complete list of signals:
<?php
define
('SIGHUP',1);
define ('SIGINT',2);
define ('SIGQUIT',3);
define ('SIGILL',4);
define ('SIGTRAP',5);
define ('SIGABRT',6);
define ('SIGBUS',7);
define ('SIGFPE',8);
define ('SIGKILL',9);
define ('SIGUSR1',10);
define ('SIGSEGV',11);
define ('SIGUSR2',12);
define ('SIGPIPE',13);
define ('SIGALRM',14);
define ('SIGTERM',15);
define ('SIGCHLD',17);
define ('SIGCONT',18);
define ('SIGSTOP',19);
define ('SIGTSTP',20);
define ('SIGTTIN',21);
define ('SIGTTOU',22);
define ('SIGURG',23);
define ('SIGXCPU',24);
define ('SIGXFSZ',25);
define ('SIGVTALRM',26);
define ('SIGPROF',27);
define ('SIGWINCH',28);
define ('SIGIO',29);
define ('SIGPWR',30);
define ('SIGSYS',31);
?>
of course there are others but this are the most important
jack at net-md dot net
09-Sep-2003 10:05
the signal NAME number mapping seems to be missing (not defined)
so the easy way to grab this info is something like:
egrep '^#define[[:space:]]SIG' /usr/include/asm/signal.h <- for linux
egrep '^#define[[:space:]]SIG' /usr/include/sys/signal.h <- for freebsd
The signals may not the same across all unix'es so be warned. also kill -l
may be the easy awnser (depending on your shell type, bash and ksh seem
to work) Here is an example:

<?
define
("SIGTERM",15); /* from /usr/include/asm/signal.h: *
                                           * #define SIGTERM        15        */
$mypid=posix_getpid();
print
"killing: ".$mypid."\n";
                                                                              
posix_kill($mypid, SIGTERM);
?>

The example might be silly, but its usefull for batch programs that need
to terminate/notify processes.
gid at gifpaste dot net
18-Dec-2002 08: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";
   }
}