防止 systemd 每次启动同一服务时写入系统日志

防止 systemd 每次启动同一服务时写入系统日志

我有一个每 30 秒运行一次的服务。该服务的systemd计时器文件如下:

~# cat /etc/systemd/system/speed_check.timer
[Unit]
Description="Check device speed"

[Timer]
OnBootSec=1
OnUnitActiveSec=30

[Install]
WantedBy=multi-user.target

服务文件只是调用一个Python脚本:

# cat /etc/systemd/system/speed_check.service 
[Unit]
Description=Check device speed

[Service]
Type=simple
ExecStart=/usr/bin/check-speed

每隔 30 秒,当 systemd 启动服务时,它会打印两条日志:

Jan  8 17:54:23 localhost systemd[1]: Starting Check device speed...
Jan  8 17:54:23 localhost systemd[1]: Started Check device speed.

有没有办法抑制这些消息?他们正在填满我的系统日志!

谢谢

答案1

有几种可能性。您应该配置该文件/etc/systemd/journald.conf。两个相关选项(来自手册页)是:

RateLimitInterval=、RateLimitBurst=

 Configures the rate limiting that is applied to all messages generated on the system. If, in the time interval
       defined by RateLimitInterval=, more messages than specified in RateLimitBurst= are logged by a service, all
       further messages within the interval are dropped until the interval is over. A message about the number of
       dropped messages is generated. This rate limiting is applied per-service, so that two services which log do not
       interfere with each other's limits. Defaults to 1000 messages in 30s. The time specification for
       RateLimitInterval= may be specified in the following units: "s", "min", "h", "ms", "us". To turn off any kind of
       rate limiting, set either value to 0.

存储=

      Controls where to store journal data. One of "volatile", "persistent", "auto" and "none". If "volatile", journal
       log data will be stored only in memory, i.e. below the /run/log/journal hierarchy (which is created if needed).
       If "persistent", data will be stored preferably on disk, i.e. below the /var/log/journal hierarchy (which is
       created if needed), with a fallback to /run/log/journal (which is created if needed), during early boot and if
       the disk is not writable.  "auto" is similar to "persistent" but the directory /var/log/journal is not created if
       needed, so that its existence controls where log data goes.  "none" turns off all storage, all log data received
       will be dropped. Forwarding to other targets, such as the console, the kernel log buffer or a syslog daemon will
       still work however. Defaults to "auto".

因此,您可以通过以下方式关闭所有系统日志存储=无,但这似乎有些过分且不明智。限制速率(第一个选项 = 更明智;默认为每 30 秒 1000 条消息,这远远超出了您的期望,并解释了为什么您有如此多的输出,未经检查。

或者,您可以使用以下选项之一,

   SystemMaxUse=, SystemKeepFree=, SystemMaxFileSize=, RuntimeMaxUse=, RuntimeKeepFree=, RuntimeMaxFileSize=

控制存储的日志文件的大小。您可以将这些选项与其中之一配对,

最大文件安全数=

      The maximum time to store entries in a single journal file before rotating to the next one

最大保留秒=

      The maximum time to store journal entries.

保留所有系统日志消息,但时间较短。

最后,您可能希望使用:

MaxLevelStore=、MaxLevelSyslog=、MaxLevelKMsg=、MaxLevelConsole=、MaxLevelWall=

      Controls the maximum log level of messages that are stored on disk, forwarded to syslog, kmsg, the console or
       wall (if that is enabled, see above). As argument, takes one of "emerg", "alert", "crit", "err", "warning",
       "notice", "info", "debug" or integer values in the range of 0..7 (corresponding to the same levels). Messages
       equal or below the log level specified are stored/forwarded, messages above are dropped. Defaults to "debug" for
       MaxLevelStore= and MaxLevelSyslog=, to ensure that the all messages are written to disk and forwarded to syslog.
       Defaults to "notice" for MaxLevelKMsg=, "info" for MaxLevelConsole= and "emerg" for MaxLevelWall=.

放弃不太重要的消息,只保留关键的消息。

答案2

我建议不要使用 rsyslog 过滤器过滤这些消息,而不是全局限制 syslogd 的速率。

echo 'if $programname == "systemd" and ($msg contains "Check device speed") then stop' >/etc/rsyslog.d/ignore-systemd-check-speed.conf
systemctl restart rsyslog

答案3

如果您的 systemd 版本是 240 或更高版本systemctl --version,那么您可以重定向 stdout 和 stderr,而无需调用 syslog 守护程序的服务或syslogd全局更改。在您的文件speed_check.service中添加以下选项[Service]

StandardOutput=append:/var/log/speed_check-stdout.log
StandardError=append:/var/log/speed_check-stderr.log

.log如果文件不存在,上面的配置将创建该文件并以附加模式打开它。如果您想在每次服务启动时截断日志,请替换append:file:

相关内容