如何将 systemd 服务的输出重定向到文件

编程入门 行业动态 更新时间:2024-10-18 10:34:53
本文介绍了如何将 systemd 服务的输出重定向到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将 systemd 服务的输出重定向到一个文件,但它似乎不起作用:

I am trying to redirect output of a systemd service to a file but it doesn't seem to work:

[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

请纠正我的做法.

推荐答案

我认为有一个更优雅的方法来解决这个问题:使用标识符将 stdout/stderr 发送到 syslog 并指示您的 syslog 管理器按程序拆分其输出名字.

I think there's a more elegant way to solve the problem: send the stdout/stderr to syslog with an identifier and instruct your syslog manager to split its output by program name.

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

Use the following properties in your systemd service unit file:

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

然后,假设您的发行版使用 rsyslog 来管理 syslog,请在 /etc/rsyslog.d/.conf 中创建一个文件,内容如下:

Then, assuming your distribution is using rsyslog to manage syslogs, create a file in /etc/rsyslog.d/<new_file>.conf with the following content:

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

现在使日志文件可由 syslog 写入:

Now make the log file writable by syslog:

# ls -alth /var/log/syslog -rw-r----- 1 syslog adm 439K Mar 5 19:35 /var/log/syslog # chown syslog:adm /path/to/log/file.log

重启 rsyslog (sudo systemctl restart rsyslog) 并享受吧!您的程序 stdout/stderr 仍可通过 journalctl (sudo journalctl -u ) 访问,但它们也将在您选择的文件中可用.

Restart rsyslog (sudo systemctl restart rsyslog) and enjoy! Your program stdout/stderr will still be available through journalctl (sudo journalctl -u <your program identifier>) but they will also be available in your file of choice.

来源来自archive

更多推荐

如何将 systemd 服务的输出重定向到文件

本文发布于:2023-07-06 07:37:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   重定向   文件   systemd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!