微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将systemd服务的输出redirect到文件

我正在尝试将systemd服务的o / predirect到一个文件,但似乎不起作用。 我这样做如下:

[Unit] Description=customprocess After=network.target [Service] Type=forking ExecStart=/usr/local/bin/binary1 agent -config-dir /etc/sample.d/server StandardOutput=/var/log1.log StandardError=/var/log2.log Restart=always [Install] WantedBy=multi-user.target

请build议正确的方法将o / predirect到一个文件

阻止recv()返回小于请求的字节的情况

用Mono + gtk打开认浏览器#

在Linux shell上并行运行多个Gradle命令

while循环同步

从Linux发行版的gem安装gem的优点和缺点?

我认为有一个更好的方法解决这个问题:用标识符发送stdout / stderr到系统日志,并指示你的系统日志管理器通过程序名来分割它的输出

在systemd服务单元文件中使用以下属性

StandardOutput=syslog StandardError=syslog SyslogIdentifier=<your program identifier> # without any quote

然后,假设您的发行版使用rsyslog来管理系统日志,请在/etc/rsyslog.d/<new_file>.conf使用以下内容创建一个文件

if $programname == '<your program identifier>' then /path/to/log/file.log if $programname == '<your program identifier>' then ~

重新启动rsyslog(sudo systemctl restart rsyslog)并享受! 你的stdout / stderr程序仍然可以通过journalctl(sudo journalctl -u)使用,但是它们也可以在你选择的文件中使用。

资料来源: http : //wiki.rsyslog.com/index.PHP/Filtering_by_program_name

你可能会得到这个错误

Failed to parse output specifier,ignoring: /var/log1.log

从systemd.exec(5)手册页:

StandardOutput=

控制执行进程的文件描述符1(STDOUT)连接到的位置。 采用inherit , null , tty , journal , syslog , kmsg , journal+console , syslog+console , kmsg+console或socket 。

systemd.exec(5)手册页介绍了与日志相关的其他选项。 另请参阅systemd.service(5)和systemd.unit(5)手册页。

或者,也许你可以尝试这样的事情(全部在一行上):

ExecStart=/bin/sh -c '/usr/local/bin/binary1 agent -config-dir /etc/sample.d/server 2>&1 > /var/log.log'

如果由于某种原因不能使用rsyslog,这将执行: ExecStart=/bin/bash -ce "exec /usr/local/bin/binary1 agent -config-dir /etc/sample.d/server >> /var/log/agent.log 2>&1"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐