LoginSignup
7
8

More than 5 years have passed since last update.

systemdでサービスを動かす際、/var/runにディレクトリを作った時の落とし穴と対策

Last updated at Posted at 2017-07-14

環境

CentOS 7

どんな状況?

  • systemdでサービスを起動したい。
  • その時の実行権限がrootじゃない。
  • 故に /var/run/[dir] を作成。
    • もちろん chown で[dir]の権限を実行ユーザにしている。
  • 作成した[dir]に、pidファイルやsockファイルが作られたりする。

何が問題?

対策

/etc/systemd/system/[サービス名].service

PermissionsStartOnly=true
ExecStartPre=/usr/bin/mkdir /var/run/[dir]
ExecStartPre=/usr/bin/chown -R [実行ユーザ]:[実行グループ] /var/run/[dir]
ExecStopPost=/usr/bin/rm -rf /var/run/[dir]

サービス起動前に[dir]を作成
サービス終了時に[dir]を削除(次回起動時にエラーになるため)
参考 : Handling /var/run with systemd
参考 : Systemd入門(4) - serviceタイプUnitの設定ファイル

7
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8