首页 > 解决方案 > 限制 systemd 中“journalctl 服务”的日志 - Centos 7

问题描述

我有一个服务**/etc/systemd/sysem/horses.service**

[Unit]
Description=Description for sample script goes here
After=rc-local.service

[Service]
Type=simple
ExecStart=/opt/horses/startSanic2.sh
TimeoutStartSec=0
Environment="REDIS_HOST=192.168.150.220"
Environment="REDIS_PORT=6379"

[Install]
WantedBy=default.target

它使用以下代码运行 shell 脚本:

#!/bin/bash
gunicorn horses.server:app --bind 0.0.0.0:9000 --worker-class sanic.worker.GunicornWorker --reload

我成功运行它,我可以看到正在写入服务的日志并且可以从以下位置读取:

journalctl -u horses.service

如果我运行该命令 - 我可以看到 horses.service 记录的所有事件。

我对 systemd 服务的日志没有任何经验。我非常担心的是,因为该服务将一直运行 - 例如是否可以设置将信息(日志)保留在 journalctl -u horses.service 中,例如仅过去 10 天,或者定义一些回合日志记录或建议的任何方法,以确保我的日志不会满,因为我确实想以我的整个磁盘(分区)满结束。请建议我如何限制日志。

先感谢您!!!!!!!

标签: loggingservicecentos7systemdsystemd-journald

解决方案


你通过运行看到的journalctl -u是你的应用程序的输出到控制台(或错误),它systemd会为你处理它们。

可以在此处找到全局配置/etc/systemd/journald.conf,默认值将满足您的需求,因此您可以使用默认值。

顺便说一句,以下是可用于更改日志记录行为的配置:

  • SystemKeepFree:为其他应用程序留出多少磁盘空间(默认值 = 15%)。

  • SystemMaxuse:最大磁盘空间使用率(默认 = 10%)。

默认情况下systemd,尽可能保留日志(受上述规则限制),但是如果您需要基于轮换的配置,您可以使用MaxRetentionSec.

同样,您无需担心磁盘使用情况systemd,只需使用默认配置即可。


推荐阅读