大网数据广告活动
服务器运维问答

Systemd service logs twice when it stops & starts. Anyway to disable?

· 52CE 我爱测

I have a systemd (v249) service ( my-script.service ) which is run every minute by another systemd timer ( my-script.timer ). Everything's working fine. But systemd logs (to it's journald & thence to /var/log/syslog ) the followi…

问题原文

原帖链接 · Amandasaurus · CC BY-SA 4.0

I have a systemd (v249) service (my-script.service) which is run every minute by another systemd timer (my-script.timer). Everything's working fine. But systemd logs (to it's journald & thence to /var/log/syslog) the following 2 lines saying that it started and then stopped it:

Aug 11 11:46:37 hostname1 systemd[1]: Started my-scripts.
Aug 11 11:46:37 hostname1 systemd[1]: my-scripts.service: Deactivated successfully.

Is there anyway to disable this logging message for this .service/.timer? Nearly all the time there is no output to log (the script starts up, checks if it has anything to do, then exits in the common case of nothing to do). These log messages are useless, and worsen the signal to noise ratio of my logs.

I am relatively new to systemd, this might be a simple question.

已采纳解答

原帖链接 · J.M. Robles · CC BY-SA 4.0

The criticality of log messages can be, as you may find in the documentation of journald

emerg (lowest log level, only highest priority messages), alert, crit, err, warning, notice, info, debug (highest log level, also lowest priority messages)

EDITED - changed the default criticality with a lower with "the default criticality value with a higher"

You can configure the default criticality of your service and the maximum criticality value you want to see in the logs editing the unit file. So, if you configure the default criticality value with a higher value than the maximum criticality (highest priority has 0 value, debug priority has a value of 7), the messages for the starting and the ending of the service wouldn't be displayed in the logs

EDITED - Changed the level representation with numbers

[Service]
...
LogLevelMax=1
SyslogLevel=2

You should produce logs in your service with a higher priority (with a lower priority value) than LogLevelMax (see Systemd for Developers III)

EDITED - ADDED EXAMPLE

I have made a new trivial service /root/Bin/test.bash that produces logs with Level 1 (alert)

#! /bin/bash
echo "<1>Done"

I have created a new unit (/etc/systemd/system/jmr.service):

[Unit]
Description=JMR test

[Service]
ExecStart=/root/Bin/test.bash
LogLevelMax=1
SyslogLevel=2

[Install]
WantedBy=multi-user.target

After that, I reconfigure systemd and restart the service two times

# systemctl daemon-reload
# systemctl start jmr.service
# systemctl start jmr.service

I can see that the logs only show the message "Done" (nothing in between the two logs)

# journalctl -u jmr.service | tail -n 2
ago 16 19:09:10 enreda test.bash[415488]: Done
ago 16 19:11:42 enreda test.bash[415620]: Done

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-08-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。