2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ユーザレベルで systemd のユニットファイルを書くときの注意点

Posted at

はじめに

systemd はシステムワイドでの起動とユーザレベルでの起動の 2 種類ある。

Shell
(sudo) systemctl <SUB_COMMAND> <SERVICE_NAME> # system-wide
systemctl --user <SUB_COMMAND> <SERVICE_NAME> # user-level

ユニットファイル (*.service) を書くとき、システムワイドでは問題ないがユーザレベルだと使えなかったり正しく起動できないものがあるのでまとめておく。

User / Group は使えない

システムワイドでは、どのユーザでデーモンを起動するかを指定するために、UserGroup というのを使うことがあるが、ユーザレベルでは使えない。

[Service]
User=ubuntu
Group=ubuntu

発生する問題

ユーザレベルで上記の項目を書くと、以下のようなエラーが発生する。

Log
foo.service: Failed to determine supplementary groups: Operation not permitted
foo.service: Failed at step GROUP spawning /usr/bin/env: Operation not permitted

解決策

単に UserGroup を削除すれば良い。

削除後はデーモンのリロードを忘れずに。

Shell
systemctl --user daemon-reload

multi-user.target は使えない

システムワイドでは WantedBymulti-user.target を指定することがあるが、これもユーザレベルでは使えない。

[Install]
WantedBy=multi-user.target

発生する問題

これが書かれていてもデーモンの起動自体はできるが、自動起動を有効にしていてもシステム起動時のデーモンの自動起動が効かなくなる。

解決策

代わりに、default.target が使える。

[Install]
WantedBy=default.target

ちなみに、上記の間違いを修正する際、すでに自動起動を有効にしている場合は、いったん無効にしてもう一度有効にする必要がある。もちろんデーモンのリロードも必要。

Shell
systemctl --user daemon-reload
systemctl --user disable <SERVICE_NAME>
systemctl --user enable <SERVICE_NAME>

正しく追加されているか確認するには、以下のコマンドを実行する。

Shell
systemctl --user list-dependencies default.target

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?