如何将Systemd服务的输出重定向到文件 [英] How to redirect output of systemd service to a file

查看:1327
本文介绍了如何将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/<new_file>.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 <your program identifier>)获得,但它们也将在您选择的文件中提供.

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.org来源

这篇关于如何将Systemd服务的输出重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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