如何记录由 start-stop-daemon 启动的进程的标准输出? [英] How can I log the stdout of a process started by 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 直接调用 shellscript 而不是 $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-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屋!