SystemdのService
linuxSystemdはLinuxで動くServiceの管理などを行うデーモン。initの後継でchkconfig/servicceの代わりにsystemctlコマンドを使う。
$ systemctl start name.service
$ systemctl stop name.service
$ systemctl status name.service
$ systemctl enable name.service
$ systemctl disable name.service
Serviceの一覧を見る。
$ systemctl list-units --type service --all
UNIT LOAD ACTIVE SUB DESCRIPTION
accounts-daemon.service loaded active running Accounts Service
acpid.service loaded active running ACPI event daemon
apparmor.service loaded active exited LSB: AppArmor initialization
apport.service loaded active exited LSB: automatic crash report generation
$ systemctl list-unit-files --type service
UNIT FILE STATE
accounts-daemon.service enabled
acpid.service disabled
ap[email protected] static # WantedByがない
apt-daily-upgrade.service static
Serviceは次のようなファイルで定義される。
$ systemctl cat ssh
# /lib/systemd/system/ssh.service
[Unit]
Description=OpenBSD Secure Shell server
After=network.target auditd.service # 対象Unitがactiveになった後に開始する
# Wants= # 対象Unitを開始しactiveかどうかにかかわらずその後に開始する
# 対象ファイルが存在している(!がついていたらいない)場合開始する
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run
[Service]
EnvironmentFile=-/etc/default/ssh # 環境変数の設定。ただしファイル内で環境変数は使えない。
ExecStartPre=/usr/sbin/sshd -t # 起動する前に実行するコマンド
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS # 起動するとき
ExecReload=/usr/sbin/sshd -t # リロードするとき
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify
[Install]
WantedBy=multi-user.target # enableにしたとき対象UnitのWantsにこのUnitが加わって自動起動するようになる
Alias=sshd.service
簡単なServiceを動かしてみる。
$ cat /lib/systemd/system/my-test.service
[Unit]
Description=My test service
[Service]
ExecStart=/usr/bin/yes
KillMode=process
Restart=on-failure
Type=simple
[Install]
WantedBy=multi-user.target
$ systemctl start my-test.service
$ systemctl status my-test.service -l
● my-test.service - My test service
Loaded: loaded (/lib/systemd/system/my-test.service; disabled; vendor preset: enabled)
Active: active (running) since Wed 2019-08-30 22:20:46 JST; 58s ago
Main PID: 26063 (yes)
Tasks: 1
Memory: 88.0K
CPU: 13ms
CGroup: /system.slice/my-test.service
└─26063 /usr/bin/yes
Aug 30 22:21:16 150-95-202-235 yes[26063]: y
Aug 30 22:21:16 150-95-202-235 yes[26063]: y
Aug 30 22:21:16 150-95-202-235 yes[26063]: y
Aug 30 22:21:16 150-95-202-235 yes[26063]: y
Aug 30 22:21:16 150-95-202-235 yes[26063]: y
Aug 30 22:21:16 150-95-202-235 yes[26063]: y
Aug 30 22:21:16 150-95-202-235 yes[26063]: y
Aug 30 22:21:16 150-95-202-235 yes[26063]: y
Aug 30 22:21:16 150-95-202-235 yes[26063]: y
Aug 30 22:21:16 150-95-202-235 yes[26063]: y
systemd-journald.serviceがsystemdのログを書いていて、journalctlで見ることもできる。
$ journalctl -u my-test.service -f
-- Logs begin at Wed 2019-08-30 23:40:16 JST. --
Aug 30 23:43:46 150-95-202-235 yes[26063]: y
Aug 30 23:43:46 150-95-202-235 yes[26063]: y
Aug 30 23:43:46 150-95-202-235 yes[26063]: y
Aug 30 23:43:46 150-95-202-235 yes[26063]: y
Aug 30 23:43:46 150-95-202-235 yes[26063]: y
Aug 30 23:43:46 150-95-202-235 yes[26063]: y
Aug 30 23:43:46 150-95-202-235 yes[26063]: y
Aug 30 23:43:46 150-95-202-235 yes[26063]: y
Aug 30 23:43:46 150-95-202-235 yes[26063]: y
Aug 30 23:43:46 150-95-202-235 yes[26063]: y
*.service.d/
下に*.conf
ファイルを置くとoverrideすることができ、次のようなファイルを置くと環境変数を渡すことができる。
# systemctl edit my-test.service
$ cat /lib/systemd/system/my-test.service.d/myenv.conf
[Service]
Environment="AAA=BBB"
参考
10.3. systemd ターゲットでの作業 Red Hat Enterprise Linux 7 | Red Hat Customer Portal
How to set environment variable in systemd service? - Server Fault