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

问题描述 投票:108回答:6

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

[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

请纠正我的方法。

linux centos7 systemd rhel rhel7
6个回答
175
投票

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

在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
& stop

现在使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>)获得,但它们也可以在你选择的文件中找到。

Source via archive.org


52
投票

如果你有一个较新的发行版systemdsystemd version 236 or newer),你可以将StandardOutputStandardError的值设置为file:YOUR_ABSPATH_FILENAME


很长的故事:

在较新版本的systemd中有一个相对较新的选项(the github request is from 2016 ish and the enhancement is merged/closed 2017 ish),您可以将StandardOutputStandardError的值设置为file:YOUR_ABSPATH_FILENAMEfile:path选项记录在most recent systemd.exec man page中。

这个新功能相对较新,因此不适用于像centos-7(或之前的任何中心)这样的旧版本。


40
投票

您可能会收到此错误:

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

来自systemd.exec(5)手册页:

StandardOutput=

控制已执行进程的文件描述符1(STDOUT)所连接的位置。取得inheritnullttyjournalsyslogkmsgjournal+consolesyslog+consolekmsg+consolesocket之一。

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' 

14
投票

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


8
投票

我建议在systemd stdout文件中添加stderrservice文件。

参考:https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput=

正如您所配置的那样,它应该不喜欢:

StandardOutput=/home/user/log1.log
StandardError=/home/user/log2.log

它应该是:

StandardOutput=file:/home/user/log1.log
StandardError=file:/home/user/log2.log

当您不想一次又一次地重新启动服务时,此方法有效。

这将创建一个新文件,但不会附加到现有文件。

使用相反:

StandardOutput=append:/home/user/log1.log
StandardError=append:/home/user/log2.log

注意:确保您已创建目录。我想它不支持创建目录。


0
投票

假设日志已经被放到stdout / stderr,并且有systemd单元登录/var/log/syslog

journalctl -u unitxxx.service

Jun 30 13:51:46 host unitxxx[1437]: time="2018-06-30T11:51:46Z" level=info msg="127.0.0.1
Jun 30 15:02:15 host unitxxx[1437]: time="2018-06-30T13:02:15Z" level=info msg="127.0.0.1
Jun 30 15:33:02 host unitxxx[1437]: time="2018-06-30T13:33:02Z" level=info msg="127.0.0.1
Jun 30 15:56:31 host unitxxx[1437]: time="2018-06-30T13:56:31Z" level=info msg="127.0.0.1

配置rsyslog(系统日志服务)

# Create directory for log file
mkdir /var/log/unitxxx

# Then add config file /etc/rsyslog.d/unitxxx.conf

if $programname == 'unitxxx' then /var/log/unitxxx/unitxxx.log
& stop

重启rsyslog

systemctl restart rsyslog.service
© www.soinside.com 2019 - 2024. All rights reserved.