如何重定向从Debian上的start-stop-daemon启动的进程的进程输出?
有几个关于这个问题,但他们都没有工作。我有一个正在停机的生产系统,我需要能够快速从守护进程获取stderr输出来调试它。如何重定向从Debian上的start-stop-daemon启动的进程的进程输出?
我以为我可以重定向它的输出点(在init.d脚本中),但它证明是非常困难的。
start-stop-daemon -d $DDIR -b -m --start --quiet -pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS > /var/log/daemon.log 2>&1 \
|| return 2
这是行不通的。我试着运行一个调用可执行文件并重定向输出的shell脚本,但仍然是空的日志文件(我知道这个过程正在输出信息)。
任何帮助将不胜感激。
至于我记得,这是不可能的,通常当我需要从一个守护进程中获取数据,我不是手之前登录,或创建一个通过网络套接字或命名管道或其他任何连接到程序的监控程序进程间通信机制。
下面是可能工作的解决方案(基于给出的解决方案here)。
在你的init.d脚本(和头后)的开头,添加以下内容:
exec > >(tee --append /var/log/daemon.log)
#You may also choose to log to /var/log/messages using this:
#exec > >(logger -t MY_DAEMON_NAME)
#And redirect errors to the same file with this:
exec 2>&1
这将记录您的脚本中都被调用,包括start-stop-daemon
输出。
调用启动 - 停止 - 守护与> /var/log/daemon.log 2>&1
将重定向启动 - 停止守护的输出和不启动守护程序的输出。 启动 - 停止后台程序将在运行守护程序之前关闭标准输出/输入描述符。
结束语可执行在这样一个简单的shell脚本:
#!/bin/bash
STDERR=$1
shift
DAEMON=$1
shift
$DAEMON 2>$STDERR $*
对我的作品 - 也许你应该检查文件的权限?
这个天真的解决方案有一个问题 - 当启动 - 停止 - 守护进程杀死这个包装时,包装的守护进程将保持活动状态。这在bash中不容易解决,因为在脚本执行期间无法运行信号处理程序(有关详细信息,请参阅trap
文档)。你必须写一个如下所示的C包装:
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char** argv){
int fd_err;
fd_err = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC);
dup2(fd_err, STDERR_FILENO);
close(fd_err);
return execvp(argv[2], argv + 2);
}
(为了清晰起见,我省略了错误检查)。
如果您有start-stop-daemon> = version 1.16.5,您只需调用它--no-close
即可重定向开始的进程的输出。
从man start-stop-daemon
:
-C
Do not close any file descriptor when forcing the daemon into the background (since version 1.16.5). Used for debugging purposes to see the process output, or to redirect file descriptors to log the process output. Only relevant when using --background.
那是我需要的解决方案。谢谢 :) –
你不能提高你的后台程序的源代码,使用'的syslog(3)'设施,--no关闭? AFAIK'start-stop-daemon'就像'daemon(3)''noclose = 0',所以关闭'stdout'&'stderr'(将它们重定向到'/ dev/null') –
[This](https:/ /stackoverflow.com/questions/8251933/how-can-i-log-the-stdout-of-a-process-started-by-start-stop-daemon/33606496#33606496)似乎工作... – Onlyjob