飘在云端

东西南北,海角天涯

· 未定义 · · 525次浏览

CentOS 8 把服务 systemctl 运行的服务日志输出到文件

更新:2022-1-24
前置要求:该字段参数 需要 systemctl ≥ 236 以上版本才支持,输入 systemctl --version 命令查看版本

经测试 CentOS 7.9 内置 systemctl 版本为 219,所以不支持该参数,请升级 systemctl
CentOS 8 内置 systemctl 为 239,支持以上参数

在不支持的 systemctl 使用,会报错:

[/etc/systemd/system/sub.service:13] Failed to parse output specifier, ignoring: ......
[/etc/systemd/system/sub.service:14] Failed to parse output specifier, ......


主要是添加这 2 个字段: StandardOutput,StandardError
示例:

[Unit]
Description=test service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/root/test/test.sh
WorkingDirectory=/root/test
Restart=always
RestartSec=3
StandardOutput=append:/root/test.log
StandardError=append:/root/test_error.log
#append 是写入日志时只追加不覆盖
#如果需要覆盖,则把这 2 行改为 file 参数,示例: StandardOutput=file:/root/test.log
#StandardError=file:/root/test_error.log
 
[Install]
WantedBy=multi-user.target

有个缺点,就是日志不会在 systemctl status -l xxxx 显示出来了,使用 tail -n=100 -f <PATH> 查看具体日志文件

评论 (0条)