LoginSignup
2
1

More than 1 year has passed since last update.

UnitファイルにおけるEnvironmentとEnvironmentFileの挙動

Last updated at Posted at 2022-02-26

はじめに

半分、備忘録的な書き方をしているため、記事を通して全体的に適当感があるかもしれませんが、ご愛嬌ください。
記事を書くきっかけとなったのは、ProftpdのソースインストールされたUnitファイルに、環境変数を読み込む設定が2つあったため、気になり調べてみました。

ソースインストールしたときに作成される?Unitファイル

make installした際にproftpd.serviceが作成されるのか、元々用意されているのか不明なため、↑のような表現をしています。
下記がproftpdのUnitファイルですが、EnvironmentEnvironmentFileの2つがあることがわかりますね。

# cat /usr/local/src/proftpd-1.3.7c/contrib/dist/rpm/proftpd.service
[Unit]
Description = ProFTPD FTP Server
Wants=network-online.target
After=network-online.target nss-lookup.target local-fs.target remote-fs.target

[Service]
Type = simple
Environment = PROFTPD_OPTIONS=
EnvironmentFile = -/etc/sysconfig/proftpd
ExecStartPre = /usr/sbin/proftpd --configtest
ExecStart = /usr/sbin/proftpd --nodaemon $PROFTPD_OPTIONS
ExecReload = /bin/kill -HUP $MAINPID
PIDFile = /run/proftpd/proftpd.pid

[Install]
WantedBy = multi-user.target

頭の良い人なら、ここでman systemd.execして、パパッと理解するかもしれませんが、僕はそんなことないので、挙動を確認するためにUnitファイルを作っていこうと思います、、、、。

実際に確認していくよ

環境変数を何も指定しない場合

はじめに下記をコピペしてください。

cat <<"EOF" > /etc/systemd/system/my-test.service
[Unit]
Description=my-test

[Service]
Environment = OPTIONS=
EnvironmentFile = -/etc/sysconfig/mytest
ExecStart=/bin/echo $OPTIONS
EOF

サービスを起動してみると、OPTIONSには何も入っていないため、表示されないですね。

# systemctl start my-test.service
# journalctl -u my-test.service
 2月 27 00:54:53 tabi systemd[1]: /etc/systemd/system/my-test.service:1: Assignment outside of section. I>
 2月 27 00:54:53 tabi systemd[1]: Started my-test.service.
 2月 27 00:54:53 tabi systemd[1]: my-test.service: Succeeded.

Environemtで環境変数を定義する場合

下記をコピペしてください。

cat <<"EOF" > /etc/systemd/system/my-test.service
[Unit]
Description=my-test

[Service]
Environment = OPTIONS=tabi
EnvironmentFile = -/etc/sysconfig/mytest
ExecStart=/bin/echo $OPTIONS
EOF

サービスを起動してみると、下記のようにEnvironemtで定義したtabiが出力されています。

# systemctl start my-test.service
# journalctl -u my-test.service
 2月 27 01:04:49 tabi systemd[1]: Started my-test.
 2月 27 01:04:49 tabi echo[33607]: tabi
 2月 27 01:04:49 tabi systemd[1]: my-test.service: Succeeded

EnvironmentFileで環境変数を定義する場合

下記をコピペしてください。

cat <<EOF > /etc/sysconfig/mytest
OPTIONS=sashiko
EOF

サービスを起動してみると、下記のようにEnvironmentFileで定義したsashikoが出力されています。

# systemctl start my-test.service
# journalctl -u my-test.service
 2月 27 01:08:24 tabi systemd[1]: Started my-test.
 2月 27 01:08:24 tabi echo[33635]: sashiko
 2月 27 01:08:24 tabi systemd[1]: my-test.service: Succeeded.

(おまけ)EnvironmentFileで環境変数の中身を定義しない場合

下記をコピペしてください。

cat <<EOF > /etc/sysconfig/mytest
OPTIONS=
EOF

サービスを起動してみると、何も表示されませんでした。

# systemctl start my-test.service
# journalctl -u my-test.service
 2月 27 01:13:22 tabi systemd[1]: Started my-test.
 2月 27 01:13:22 tabi systemd[1]: my-test.service: Succeeded.

結論

ProftpdのUnitファイルでやっていること

/etc/sysconfig/proftpdに何も定義していなければ、下記のようなコマンドが実行されるみたいです。

  • /usr/sbin/proftpd --nodaemon
# cat /usr/local/src/proftpd-1.3.7c/contrib/dist/rpm/proftpd.service
[Unit]
Description = ProFTPD FTP Server
Wants=network-online.target
After=network-online.target nss-lookup.target local-fs.target remote-fs.target

[Service]
Type = simple
Environment = PROFTPD_OPTIONS=
EnvironmentFile = -/etc/sysconfig/proftpd
ExecStartPre = /usr/sbin/proftpd --configtest
ExecStart = /usr/sbin/proftpd --nodaemon $PROFTPD_OPTIONS
ExecReload = /bin/kill -HUP $MAINPID
PIDFile = /run/proftpd/proftpd.pid

[Install]
WantedBy = multi-user.target

わかったこと

環境変数を定義する場合、Environmentよりも、EnvironmentFileのファイル内に定義した環境変数が読み込まれるらしいです。

ちなみに筆者はどっちに環境変数を定義した方が良いかは分かっていないため、「自分はこうしているよ」みたいなのあれば、コメントしていただけると幸いです。
ここまでお読みいただき、ありがとうございました!

2
1
0

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
1