Search code examples
phplinuxsyslogsyslog-ng

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


I wrote a PHP program that hooks into syslog-ng (via syslog-ng.conf) and it's basically this:

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

where process() and cleanup() are defined by me.

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

I have tried to catch 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 it runs cleanup()), but it appears that I am not getting those meessages from syslog-ng.

I have tried setting STDIN to non-blocking, thinking that PHP wouldn't call the signal handlers because the stream was blocking. That didn't work either, my signal handlers wouldn't get called.

How do I know when syslog-ng is about to stop my application, so I can do some cleanup?

Thanks, Tom

UPDATE: I've tried to catch all the signals, from 1 to 31 and it still doesn't receive anything when syslog-ng is restarted (or killed with SIGTERM).


Solution

  • Perhaps try to register cleanup() as a PHP shutdown function, like so:

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

    In theory this will cause php to execute the function prior to exiting. This may not work when used with forced halt options (like kill) though, so it's (yet another) shot in the dark.

    I hope you find something that works though. I'd be interested in learning the results as well.

    Edit: I'm glad this worked out for you!