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

I wrote a PHP program that intercepts syslog-ng(through syslog-ng.conf), and 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 it is cleanup(2)never called, and I need it to execute before the program exits.

I tried to catch SIGTERM, SIGHUP and SIGPIPE with pcntl_signal(), and this part of the application is working fine (if I use kill -1 in the PHP process, my signal handler gets called and it starts cleanup()), but it seems that I am not receiving these messages from syslog-ng.

I tried setting STDIN to non-blocking, believing that PHP would not call signal handlers because the thread was blocking. This did not work either, my signal handlers will not be called.

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

Thanks Tom

UPDATE: I tried to catch all signals from 1 to 31, and it still does not receive anything when restarting syslog-ng (or killed with SIGTERM).

+3
source share
5 answers

Perhaps try registering a function shutdowncleanup() as PHP , for example:

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

php . (, kill), , , ( ) .

, -, . .

: , !

+3

, :

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

, syslog-ng stdout ( ?), fgets EOF.

0

PHP script , , (, () , fgets(), EOF).

PHP, , .

0

, :

1) PHP set_error_handler().

2) , , try/catch, , .

3) script, , , . :

/path/to/script 2>&1 >> /path/to/logfile.txt

..., .

, , PHP , script trap. - :

#!/bin/sh
#
# This gets run when the script is hit with a signal
#
trap /path/to/script --cleanup
#
# Run our script
#
/path/to/script 2>&1 >> /path/to/logfile.txt

script, (), script .

!

0
source

Is PHP thoughtfully handling your signals for you? Try prototyping in another language so you have more control.

0
source

Source: https://habr.com/ru/post/1721528/


All Articles