简体   繁体   English

如何将子进程的输出重定向到syslog?

[英]how to redirect a child process's output to syslog?

I'm developing in C under freebsd. 我在freebsd下用C开发。 I'm using cron job to invoke a program A which uses fork()/exec() to call another program B. In A, I use syslog() to write logs to /var/log/messages, but B outputs to stdout so B's output is not written to /var/log/messages. 我正在使用cron作业来调用程序A,该程序使用fork()/ exec()来调用另一个程序B。在A中,我使用syslog()将日志写入/ var / log / messages,但是B输出到stdout因此,B的输出未写入/ var / log / messages。

How can I redirect B's output to syslog also without modifying B's code? 如何在不修改B代码的情况下也将B的输出重定向到syslog? I guess there should be some mechanism to redirect B's output to some fd in A, but since I'm using syslog() instead of opening a file directly in A, I'm not sure how this would work. 我猜应该有某种机制可以将B的输出重定向到A中的某些fd,但是由于我使用的是syslog()而不是直接在A中打开文件,因此我不确定这将如何工作。

Thanks for suggestions! 感谢您的建议!

I can't recall if freebsd includes the logger program. 我不记得freebsd是否包含记录程序。 If so, your program A might be able to invoke B as some form of "B | logger". 如果是这样,您的程序A可能可以调用B作为“ B | logger”的某种形式。 For example, instead of 例如,代替

execl("/my/path/to/B", "B", NULL);

Leverage the shell: 利用外壳:

execl("/bin/sh", "sh", "-c", "/my/path/to/B | logger", NULL);

The logger program has additional options to direct its messages to a particular syslog facility, which may be needed to match your A application's behavior. 记录器程序还有其他选项,可将其消息定向到特定的syslog工具,以匹配A应用程序的行为。

Lacking logger , you may need to do traditional Posix pipe-fitting: use pipe(2) to create a pipe, dup the output side of the pipe to B's stdout, and read (either in A, or in a separate child process) that pipe's input side for messages to send to syslog(3) 缺少记录器 ,您可能需要进行传统的Posix管道拟合:使用pipe(2)创建管道,将管道的输出端复制到B的stdout,然后读取(在A中或在单独的子进程中)管道的输入端,用于将消息发送到syslog(3)

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

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