简体   繁体   English

你怎么知道syslog-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: 我写了一个挂钩到syslog-ng (通过syslog-ng.conf )的PHP程序,基本上是这样的:

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

where process() and cleanup() are defined by me. 其中process()cleanup()由我定义。

The problem I am facing is that cleanup(2) is never called and I need it to be executed before the program exits. 我面临的问题是从不调用cleanup(2) ,我需要在程序退出之前执行它。

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. 我试图使用pcntl_signal()捕获SIGTERM,SIGHUP和SIGPIPE,并且该部分应用程序似乎工作正常(如果我在PHP进程中使用kill -1,我的信号处理程序被调用并运行cleanup() ),但是看来我没有收到syslog-ng的那些消息。

I have tried setting STDIN to non-blocking, thinking that PHP wouldn't call the signal handlers because the stream was blocking. 我已经尝试将STDIN设置为非阻塞,认为PHP不会调用信号处理程序,因为流阻塞了。 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? 我怎么知道syslog-ng什么时候停止我的应用程序,所以我可以做一些清理工作?

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). 更新:我试图捕获所有信号,从1到31,当syslog-ng重新启动(或使用SIGTERM杀死)时仍然没有收到任何信号。

Perhaps try to register cleanup() as a PHP shutdown function , like so: 也许尝试将cleanup()注册为PHP 关闭函数 ,如下所示:

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

In theory this will cause php to execute the function prior to exiting. 从理论上讲,这将导致php在退出之前执行该功能。 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! 编辑:我很高兴这对你有用!

This is a shot in the dark, but try re-writing your loop as follows: 这是一个黑暗中的镜头,但尝试重写你的循环如下:

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

The idea being that when syslog-ng closes stdout (assuming it does?), fgets will attempt to read and actually reach EOF. 想法是当syslog-ng关闭stdout时(假设确实如此?),fgets将尝试读取并实际达到EOF。

Abrupt termination of your PHP script without proper cleanup is probably because a runtime error happens (for example, because your process() is not ready to handle an empty string returned by by fgets() when it reaches EOF). 在没有正确清理的情况下突然终止PHP脚本可能是因为发生了运行时错误(例如,因为你的process()还没有准备好处理fgets()在到达EOF时返回的空字符串)。

Enable PHP error logging to see what happens. 启用PHP错误日志记录以查看发生的情况。

I can think of a couple of things you can try to troubleshoot this: 我可以想到一些你可以尝试解决这个问题的事情:

1) Make use of PHP's set_error_handler() function . 1)使用PHP的set_error_handler()函数

2) Put the code mentioned above in a try/catch structure, to try and catch any exceptions that might be thrown. 2)将上面提到的代码放在try / catch结构中,尝试捕获可能引发的异常。

3) When you run that script, make sure you are capturing both the output and standard error. 3)运行该脚本时,请确保捕获输出和标准错误。 Change the command line to be something like: 将命令行更改为:

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

...that will help you catch errors. ...这将帮助您发现错误。

Finally, if it really is a case where PHP isn't able to catch signals, you'll have to do this through a shell script with the trap command. 最后,如果确实是PHP无法捕获信号的情况,则必须通过带有trap命令的shell脚本执行此操作。 Something like: 就像是:

#!/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

If there's specific data in the script that cleanup() needs, then you'll have to see about serializing the data and writing it to disk for the cleanup script to read. 如果脚本中有cleanup()需要的特定数据,那么您将不得不看到有关序列化数据并将其写入磁盘以供清理脚本读取的信息。

Good luck! 祝好运!

Does PHP thoughtfully handle your signals for you? PHP是否为您精心处理您的信号? Try prototyping in another language that you have more control. 尝试用您可以控制的另一种语言进行原型设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM