如何记录由start-stop-daemon启动的进程的标准输出? [英] How can I log the stdout of a process started by start-stop-daemon?

查看:706
本文介绍了如何记录由start-stop-daemon启动的进程的标准输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个初始化脚本来运行一个简单的过程,该过程始于:

I am using an init script to run a simple process, which is started with:

start-stop-daemon --start --quiet --chuid $DAEMONUSER    \
    --make-pidfile --pidfile $PIDFILE --background       \
    --exec $DAEMON $DAEMON_ARGS

称为$ DAEMON的进程通常将日志信息打印到其标准输出中.据我所知,这些数据没有存储在任何地方.

The process called $DAEMON usually prints log information to its standard output. As far as I can tell this data is not being stored anywhere.

我想将$ DAEMON的标准输出写入或附加到某个文件中.

I would like to write or append the stdout of $DAEMON to a file somewhere.

我知道的唯一解决方案是告诉start-stop-daemon调用shell脚本而不是直接调用$ DAEMON.然后,脚本将调用$ DAEMON并将其写入日志文件.但这需要一个额外的脚本,就像修改守护程序本身一样,似乎是解决此类常见任务的错误方法.

The only solution I know is to tell start-stop-daemon to call a shellscript instead of $DAEMON directly; the script then calls $DAEMON and writes to the logfile. But that requires an extra script which, like modifying the daemon itself, seems the wrong way to solve such a common task.

推荐答案

扩大ypocat的答案,因为它不会让我发表评论:

To expand on ypocat's answer, since it won't let me comment:

start-stop-daemon --start --quiet --chuid $DAEMONUSER    \
 --make-pidfile --pidfile $PIDFILE --background       \
 --startas /bin/bash -- -c "exec $DAEMON $DAEMON_ARGS > /var/log/some.log 2>&1"

使用exec运行守护程序可以使stop正确停止子进程,而不仅仅是bash父进程.

Using exec to run the daemon allows stop to correctly stop the child process instead of just the bash parent.

使用--startas而不是--exec确保该进程将被其pid正确检测到,并且如果多次调用start,则不会错误地启动该守护程序的多个实例.否则,start-stop-daemon将查找/bin/bash进程,而忽略运行该守护程序的实际子进程.

Using --startas instead of --exec ensures that the process will be correctly detected by its pid and won't erroneously start multiple instances of the daemon if start is called multiple times. Otherwise, start-stop-daemon will look for a /bin/bash process and ignore the actual child process running the daemon.

这篇关于如何记录由start-stop-daemon启动的进程的标准输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆