さかもとのブログ

つらつらと

CentOS 7 で tmp fileを消してくれるserviceを停止する

RHEL7から採用されたsystemdですが、機能が豊富すぎてよくわかっていません。 とりあえずtmpファイルを消すsystemd-tmpfiles-clean.serviceを止める方法を記載します。

忙しい方向け(すぐに止めたい)

とりあえず止めたい場合は、下記のコマンドを実行してください。

# 今動いているtimerを止める
systemctl stop systemd-tmpfiles-clean.timer

# 起動時にtimerが起動しないようにする
systemctl mask systemd-tmpfiles-clean.timer

timerとか、maskとかいきなり出ていますが、興味がある方は下記へ

systemdのserviceとかtimerとか

よくわかっていません。とりあえず調べた範囲だけ書きます。

  • service
    • systemdで動かしたいコマンドの実態
    • .serviceという拡張子(と言っていいのかな..)のファイル
  • timer
    • .serviceで作成したserviceを動かすタイマー
    • ここに起動する時間などを記載する
    • つまりserviceで定義した内容をcron的に動かしたい場合に定義する
    • .timerという拡張子(と言っていいのかな..のファイル
    • serviceと同じ名前で、.timerにすると自動で認識される

tmpfileを消すserviceの中身

service, timerをざっくりと説明しましたが、tpmfileを消すserviceの中身を見てみます。

  • systemd-tmpfiles-clean.serviceの中身 (コメントは削除)
[vagrant@node1 ~]$ cat /usr/lib/systemd/system/systemd-tmpfiles-clean.service
[Unit]
Description=Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-readahead-collect.service systemd-readahead-replay.service local-fs.target time-sync.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemd-tmpfiles --clean
IOSchedulingClass=idle

Unitとか、Serviceとかありますが、これはセクションと呼ばれるもので、[Service]セクションのExcecStartに記載されている内容が、実行される内容です。

ここでは/usr/bin/systemd-tmpfiles --cleanですね。

  • systemd-tmpfiles-clean.timerの中身
[vagrant@node1 ~]$ cat /usr/lib/systemd/system/systemd-tmpfiles-clean.timer
[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)

[Timer]
OnBootSec=15min
OnUnitActiveSec=1d

Timerセクションに記載されているところが、Timerの実態です。OnBootSecは、起動後15分で、OnUnitActiveSecは、serviceの起動間隔です。

サービスを止める

まずは上記のそれぞれが、どんなstatusなのかを見てみます。

[vagrant@node1 ~]$ systemctl status systemd-tmpfiles-clean.service
● systemd-tmpfiles-clean.service - Cleanup of Temporary Directories
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.service; static; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:tmpfiles.d(5)
           man:systemd-tmpfiles(8)
[vagrant@node1 ~]$ systemctl status systemd-tmpfiles-clean.timer
● systemd-tmpfiles-clean.timer - Daily Cleanup of Temporary Directories
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.timer; static; vendor preset: disabled)
   Active: active (waiting) since 水 2016-08-24 03:54:23 BST; 7min ago
     Docs: man:tmpfiles.d(5)
           man:systemd-tmpfiles(8)

serviceのほうはinactiveになっていて、timerがactiveになっているので、このserviceはあくまでtimerによって起動されるものであることがわかります。

なので、timerをdisableにすれば止まる、と思いきや、systemd-tmpfiles-clean.timerは起動時onをoffにできません。

いくつかのsystemdのコマンドを扱ったブログでは、とにかくdisableで止めていますが、これで止まるものと止まらないものがあります。

disableで止まるもの・止まらないもの

それぞれのserviceのSTATEを見ると、disableで起動時offができるかどうかがわかります。

[vagrant@node1 ~]$ systemctl list-unit-files  -t service postfix.service
UNIT FILE       STATE
postfix.service enabled

これはdisableできます。では、systemd-tmpfiles-cleanは..?

(timerはserviceに依存するので、本当はserviceだけ見ればよいですが、一応)

[vagrant@node1 ~]$ systemctl list-unit-files  -t service systemd-tmpfiles-clean.service
UNIT FILE                      STATE
systemd-tmpfiles-clean.service static

1 unit files listed.
[vagrant@node1 ~]$ systemctl list-unit-files  -t timer systemd-tmpfiles-clean.timer
UNIT FILE                    STATE
systemd-tmpfiles-clean.timer static

staticになっていますね。で、このstaticとはman systemctlで見てみると、Table 1. is-enabled outputに (systemctl is-enabled ユニット名でも、上記のSTATEは見れます)

Unit file is not enabled, and has no provisions for enabling in the "[Install]" section.

と記載されています。つまり、staticの場合は、enableとかの管轄外なんですね。

で、もう一度systemd-tmpfiles-clean.serviceを見てみると、あれ?[Install] sectionがありません。

[vagrant@node1 ~]$ cat /usr/lib/systemd/system/systemd-tmpfiles-clean.service
[Unit]
Description=Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-readahead-collect.service systemd-readahead-replay.service local-fs.target time-sync.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemd-tmpfiles --clean
IOSchedulingClass=idle

ではどこに? Afterとあるので、このユニットはいくつかのユニットに依存しています。 で、systemd-readahead-collect.serviceを見てみると

[vagrant@node1 ~]$ cat /usr/lib/systemd/system/systemd-readahead-collect.service
[Unit]
Description=Collect Read-Ahead Data
Documentation=man:systemd-readahead-replay.service(8)
DefaultDependencies=no
Wants=systemd-readahead-done.timer
Conflicts=shutdown.target
Before=sysinit.target shutdown.target
ConditionPathExists=!/run/systemd/readahead/cancel
ConditionPathExists=!/run/systemd/readahead/done
ConditionVirtualization=no

[Service]
Type=notify
ExecStart=/usr/lib/systemd/systemd-readahead collect
RemainAfterExit=yes
StandardOutput=null
OOMScoreAdjust=1000

[Install]
WantedBy=default.target
Also=systemd-readahead-drop.service

[Install]セクションがありますね。 ということで、

  • systemd-readahead-collect.serviceは[Install]セクションで記載されたtargetのタイミングでのみ起動する
  • これに依存しているsystemd-tmpfiles-clean.serviceもstatic扱いになる
  • それのtimerもstaticになるので、disable(enableも)の管轄外
  • maskでoffにする

ということになります。

参考:http://equj65.net/tech/systemd-manage/