tags:

views:

239

answers:

3

Hi,

I have a daemon written in PHP which is running on my linux machine.

I am trying to send a signal to it through another php file. For this purpose I am trying posix_kill function. But its not working.

When I run the php page, I get an error that php is compiled without --enable-grep

I want to know how to enable it? OR what is the alternate way of sending signal to daemon?

A: 

If you know the process's ID, you could just

exec( "kill -SIGKILL 1234", $return );
print_r( $return );

Or if you don't know the process ID

exec( "pkill -KILL myDaemon", $r );
print_r( $return );

To find all the available signals you could send:

shell> kill -l

If you are having trouble, redirect stderr to standard output:

exec( "pkill -KILL myDaemon 2>&1", $r );
print_r( $return );

This will show you any error messages that would have appeared on the terminal (had you been executing the command that way!).

Andy
I know the process ID. But calling exec( "kill -SIGKILL 1234", $return ); return nothing from PHP.when I can same command from linux prompt, it works fine.what might be wrong?
Riz
the user running php must be the owner of the process. Try using the 3rd parameter to `exec()` to check the return code for errors http://php.net/manual/en/function.exec.php
Andy
No, Owner of process is root.When I call exec( "kill -SIGKILL $PID", $return, $out );$return displays blank array.$out displays 1
Riz
1 means failure. http://steve-parker.org/sh/exitcodes.shtml I'll update my answer so you can get the error message from the script by redirecting `stderr`
Andy
But how to pass signal through php to a process which is owned by root?? This is what I need. Any idea?
Riz
The user running PHP must be the owner of the daemon you want to kill. Otherwise try lock files as per other answer.
Andy
A: 

Try using shared memory, locks, or files. Sending signals between processes may not work if the process belongs to a different user.

Using files or locks for example may help you if you ever need to scale, as its easier to replicate than using signals.

The problems with signalling like i've said is that the daemon must look periodically for events... by using signals a certain trigger in the daemon would get instantly called, and that makes life easier if you need a fast response back.

Quamis
can u pls give me an example code about your suggestion?
Riz
sorry, but you have to put a little work into it too, i'm not going to simply give the code...
Quamis
A: 

The only way to handle signals on your PHP script is to re-compile PHP with the PCNTL library that will give you the tools/functions you need.

http://php.net/manual/en/book.pcntl.php

Otherwise, you need to use workarounds like Quamis mentioned : file flags or locks, etc.

Rafa