【问题标题】:Systemd logging to external file系统日志记录到外部文件
【发布时间】:2017-04-27 08:42:36
【问题描述】:

我有以下 Systemd 服务脚本来运行 Spring Boot 应用程序-

[Unit]
Description=Upstart for Security
After=network.target network-online.target
Wants=network-online.target

[Service]
User=root
WorkingDirectory=/home/ubuntu/security
ExecStart=/usr/bin/java -classpath java -Dspring.profiles.active=stage -jar /home/ubuntu/security/security-0.0.1-SNAPSHOT.jar > /home/ubuntu/security/security.log 2>&1
SuccessExitStatus=143
Restart=on-failure
RestartSec=120s

[Install]
WantedBy=multi-user.target


我将脚本保存在以下位置 -

 /etc/systemd/system


我运行了以下命令来运行 systemd 服务脚本 -

1. sudo systemctl enable security.service -or- sudo systemctl daemon-reload
2. sudo systemctl status security.service 
3. sudo systemctl start security.service


为了检查日志,我触发了命令 -

journalctl -u security.service

并使用 SHIFT+G 滚动到 eof

我可以通过上述步骤检查日志,但我希望它们位于 /home/ubuntu/security 位置的外部文件中,如 security.log
我怎样才能实现它?我要对我的 systemd 脚本进行哪些更改?

【问题讨论】:

标签: ubuntu ubuntu-16.04 systemd


【解决方案1】:

您可以使用 syslog 来完成此操作。指示您的 systemd 服务使用标识符将 stdout/stderr 路由到 syslog,并让 syslog 处理写入文件。

[Service]
...
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=<your-svc-identifier>

现在在/etc/rsyslog.d/的系统日志中添加一个配置

if $programname == '<your-svc-identifier>' then /var/log/<your-svc>.log
if $programname == '<your-svc-identifier>' then stop

重新加载系统日志

$ sudo systemctl restart rsyslog

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多