php – How do you know if syslog-ng has stopped your listening daemon?

How do you know if syslog-ng has stopped your listening daemon?… here is a solution to the problem.

How do you know if syslog-ng has stopped your listening daemon?

I wrote a PHP program that connects to syslog-ng (via syslog-ng.conf) and it basically looks like this:

while (!feof(STDIN)) {
    $input = fgets(STDIN);
    process($input);
}
cleanup();

where process() and cleanup() are my definitions.

The problem I am facing is that cleanup(2) is never called and I need to execute it before the program exits.

I

tried capturing SIGTERM, SIGHUP, and SIGPIPE using pcntl_signal() and that part of the application seems to work fine (if I use kill -1 on the PHP process, my signal handler gets called and runs cleanup()), but I don’t seem to get those messages from syslog-ng.

I have tried to set STDIN to non-blocking, thinking that PHP doesn’t call signal handlers because streams are blocking. This also doesn’t work, my signal handler won’t be called.

How do I know when syslog-ng will stop my application so that I can do some cleanup?

Thank you
Tom

UPDATE: I HAVE TRIED TO CATCH ALL SIGNALS, FROM 1 TO 31, BUT WHEN SYSLOG-NG IS RESTARTED (OR TERMINATED BY SIGTERM) IT STILL DOESN’T RECEIVE ANY SIGNALS.

Solution

Maybe try registering cleanup() as PHP shutdown function, like this:

function cleanup(){}
register_shutdown_function("cleanup");

Theoretically, this causes PHP to execute the function before exiting. However, this may not work when used with a force stop option like kill, so it (again) is filmed in the dark.

I hope you find something useful. I’m also interested to know the results.

Edit: I’m glad this worked for you!

Related Problems and Solutions