CentOS7のcronによるログStarting user-0.sliceなどをmessagesから消す方法


CentOS7でcronによるプログラム実行を有効にすると、5分毎に下記の様な出力がある。

journalctlのログには下記の様な感じで

# journalctl |grep " 2月 28 08:30:01"
 2月 28 08:30:01 blog.osakana.net systemd[1]: Started Session 745 of user username.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Starting Session 745 of user username.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Started Session 744 of user username.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Starting Session 744 of user username.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Created slice user-499.slice.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Starting user-499.slice.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Started Session 743 of user munin.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Starting Session 743 of user munin.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Created slice user-0.slice.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Starting user-0.slice.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Started Session 746 of user root.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Starting Session 746 of user root.
 2月 28 08:30:01 blog.osakana.net CROND[713]: (username) CMD (/~/script/toppage.pl >> /~/tw5-topcheck.txt 2>&1)
 2月 28 08:30:01 blog.osakana.net CROND[723]: (munin) CMD (test -x /usr/bin/munin-cron && /usr/bin/munin-cron)
 2月 28 08:30:01 blog.osakana.net CROND[724]: (root) CMD (/usr/lib64/sa/sa1 1 1)
 2月 28 08:30:01 blog.osakana.net systemd[1]: Removed slice user-0.slice.
 2月 28 08:30:01 blog.osakana.net systemd[1]: Stopping user-0.slice.
#

/var/log/messagesの方には下記の様な感じで・・・

# grep "Feb 28 08:30:01" /var/log/messages
Feb 28 08:30:01 blog.osakana.net systemd: Started Session 745 of user username.
Feb 28 08:30:01 blog.osakana.net systemd: Starting Session 745 of user username.
Feb 28 08:30:01 blog.osakana.net systemd: Started Session 744 of user username.
Feb 28 08:30:01 blog.osakana.net systemd: Starting Session 744 of user username.
Feb 28 08:30:01 blog.osakana.net systemd: Created slice user-499.slice.
Feb 28 08:30:01 blog.osakana.net systemd: Starting user-499.slice.
Feb 28 08:30:01 blog.osakana.net systemd: Started Session 743 of user munin.
Feb 28 08:30:01 blog.osakana.net systemd: Starting Session 743 of user munin.
Feb 28 08:30:01 blog.osakana.net systemd: Created slice user-0.slice.
Feb 28 08:30:01 blog.osakana.net systemd: Starting user-0.slice.
Feb 28 08:30:01 blog.osakana.net systemd: Started Session 746 of user root.
Feb 28 08:30:01 blog.osakana.net systemd: Starting Session 746 of user root.
Feb 28 08:30:01 blog.osakana.net systemd: Removed slice user-0.slice.
Feb 28 08:30:01 blog.osakana.net systemd: Stopping user-0.slice.
#

これを消すための方法をググると、 「/etc/systemd/system.confにLogLevel=noticeを書けばok」なんてのが出てきます。

しかし、これはsystemd全体のログを出力させなくするものであって、今回のログ以外にも出てこなくなってしまうものが発生します。

もっと影響範囲が狭い対処方法がないか探したところRedHatサイトで下記を発見
Logs flooded with systemd messages: Created slice & Starting Session
messagesログに systemd メッセージ Created slice や Starting Session が大量に出力される

これは、systemdによるログはrsyslogにより/var/log/messagesに記録されているが、systemdからの特定文字列が含まれるログ出力を無視する設定をrsyslogの設定ファイルに記載する、というやり方です。(journalctlの次の段階の出力を調整する手法のため、journalctlの出力結果は変わらない)

下記のコマンドで/etc/rsyslog.d/ignore-systemd-session-slice.confファイルを作成します。

(下記は2017/2/28時点でのもの)
echo 'if $programname == "systemd" and ($msg contains "Starting Session" or $msg contains "Started Session" or $msg contains "Created slice" or $msg contains "Starting user-") then stop' >/etc/rsyslog.d/ignore-systemd-session-slice.conf
(下記は2020/07/20時点でのもの)
echo 'if $programname == "systemd" and ($msg contains "Starting Session" or $msg contains "Started Session" or $msg contains "Created slice" or $msg contains "Starting user-" or $msg contains "Starting User Slice of" or $msg contains "Removed session" or $msg contains "Removed slice User Slice of" or $msg contains "Stopping User Slice of") then stop' >/etc/rsyslog.d/ignore-systemd-session-slice.conf

作成後、「systemctl restart rsyslog」でrsyslogを再起動します。

# ls -l /etc/rsyslog.d
合計 4
-rw-r--r--. 1 root root 49 11月 22 10:27 listen.conf
# cat /etc/rsyslog.d/listen.conf
$SystemLogSocketName /run/systemd/journal/syslog
# echo 'if $programname == "systemd" and ($msg contains "Starting Session" or $msg contains "Started Session" or $msg contains "Created slice" or $msg contains "Starting user-") then stop' >/etc/rsyslog.d/ignore-systemd-session-slice.conf
# ls -l /etc/rsyslog.d
合計 8
-rw-r--r--. 1 root root 180  2月 28 09:14 ignore-systemd-session-slice.conf
-rw-r--r--. 1 root root  49 11月 22 10:27 listen.conf
# cat /etc/rsyslog.d/ignore-systemd-session-slice.conf
if $programname == "systemd" and ($msg contains "Starting Session" or $msg contains "Started Session" or $msg contains "Created slice" or $msg contains "Starting user-") then stop
# systemctl restart rsyslog
#

上記の対処後の状況を見てみる

# tail /var/log/messages
Feb 28 09:20:01 blog systemd: Removed slice user-0.slice.
Feb 28 09:20:01 blog.osakana.net systemd: Stopping user-0.slice.
Feb 28 09:20:22 blog.osakana.net systemd: Removed slice user-499.slice.
Feb 28 09:20:22 blog.osakana.net systemd: Stopping user-499.slice.
#

/var/log/messages は「Removed slice」と「Stopping user-」が条件になかったので、記録されていました。

このため、下記の様に条件を追加して、対処しました。

# cat /etc/rsyslog.d/ignore-systemd-session-slice.conf
if $programname == "systemd" and ($msg contains "Starting Session" or $msg contains "Started Session" or $msg contains "Created slice" or $msg contains "Starting user-" or $msg contains "Removed slice" or $msg contains "Stopping user-") then stop
$

なお、journalctlの方は引き続き出力ありです。 (今回の対処方法では消せない)

# journalctl |tail
 2月 28 09:30:01 blog.osakana.net systemd[1]: Started Session 783 of user username.
 2月 28 09:30:01 blog.osakana.net systemd[1]: Starting Session 783 of user username.
 2月 28 09:30:01 blog.osakana.net CROND[6940]: (root) CMD (/usr/lib64/sa/sa1 1 1)
 2月 28 09:30:01 blog.osakana.net CROND[6942]: (munin) CMD (test -x /usr/bin/munin-cron && /usr/bin/munin-cron)
 2月 28 09:30:01 blog.osakana.net CROND[6943]: (username) CMD (/~/script/toppage.pl >> /~/tw5-topcheck.txt 2>&1)
 2月 28 09:30:01 blog.osakana.net systemd[1]: Removed slice user-0.slice.
 2月 28 09:30:01 blog.osakana.net systemd[1]: Stopping user-0.slice.
 2月 28 09:30:25 blog.osakana.net systemd[1]: Removed slice user-499.slice.
 2月 28 09:30:25 blog.osakana.net systemd[1]: Stopping user-499.slice.
#

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください