C/C++教程

centos 7,8 systemd service 自定义日志输出

本文主要是介绍centos 7,8 systemd service 自定义日志输出,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

背景

默认情况下, 在 centos 7, 8 上, 使用 systemd 控制服务
服务日志输出默认都由 journald 接受

目的

自定义 service 的日志输出

方法1

简单, 但进程会以子进程方式启动, 通过 shell 重定向方法控制
参考下面随意的 service 服务启动方法

[Service]
ExecStart=/bin/sh -c /apps/myprogram -c /apps/myconfig > /apps/log/mylog 2>&1

可以通过 systemctl status servicename.service 查询进程运行情况
上面情况会导致 /bin/sh 产生子进程再运行你的程序
日志控制由 shell 进行重定向

方法2

通过 rsyslog 进行日志重新定义

参考 service 语法

[Service]
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=my-pro-agent
ExecStart=/apps/myprogram -c /apps/myconfig 

参考 rsyslog 定义方法
注意, my-pro-agent

这篇关于centos 7,8 systemd service 自定义日志输出的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!