参考文献 †
Systemdってなに †
- Linux の起動プロセス
- PID 1
- 古き良き initd に代わるもの
古き良き initd †
- 今までの Unix の起動プロセス
- /etc/inittab の設定内容に従って、OS とサービスを起動
- /etc/rc.d/rc.sysinit の実行。ファイルシステムのマウントなど
- /etc/rc.d/rc3.d/ 以下のシェルスクリプトを実行
- initd では、サービスはシェルスクリプトを使って起動する
Unit †
- Systemd では、Linux の起動プロセスは Unit ファイルで記述される
Unit | Explanation |
service | バイナリを実行するUnit。サービスを起動する |
device | デバイスの有効化 |
target | Unit間の依存関係を定義するための何もしないUnit |
mount | ファイルシステムをマウントするUnit |
automount | automound 相当。普段はファイルシステムをマウントせず、ファイルシステムにアクセスがあったらマウントする |
socket | xinitd 相当。Systemd が socket を listen して、監視対象のポートへのアクセスを契機に、サービスを起動し、パケットをに受け渡す |
path | inotify 相当。監視対象のファイル作成を契機に、サービスを起動する |
- Unit 格納ディレクトリ
Direcotory | Explanation |
/etc/systemd/system | 【優先】ユーザ作成の Unit。 | /usr/lib/systemd/system に同名の Unit があった場合、/etc/systemd/system にある方が使われる |
/usr/lib/systemd/system | インストーラが作成した Unit。 |
Unit の依存関係 †
target などの固定的な依存関係 †
- Unit ファイルの [Unit] セクションに Wants/Requires を定義する
- /usr/lib/systemd/system/multi-user.target
[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes
- Systemd は、自Unit の実行に先立ち ${Unit} が実行されるようにスケジューリングする
- Wants/Requires の違い
Dependency | ${Unit} の実行が失敗した場合 |
Wants=${Unit} | 自Unit を実行する |
Requires=${Unit} | 自Unit を実行しない |
serivceのように ON/OFF を切り替えたい場合の依存関係 †
- Unit ファイルの [Install] セクションに Wanted/Requireed を定義する
- /usr/lib/systemd/system/ntpd.service
[Unit]
Description=Network Time Service
After=ntpdate.service sntp.service
Conflicts=systemd-timesyncd.service
[Service]
ExecStart=/usr/sbin/ntpd -g -n -f /var/lib/ntp/ntp.drift -u ntp:ntp
PrivateTmp=true
Restart=always
[Install]
WantedBy=multi-user.target
- ntpd.service を有効化すると /etc/systemd/system/multi-user.target.wants/ に symbolic link が作られる
$ sudo systemctl enable ntpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib64/systemd/system/ntpd.service.
- ${target}.wants ディレクトリにある Unit は、${target} から wants されている Unit になる
- service を disable すると symbolic link が削除される
$ sudo systemctl disable ntpd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/ntpd.service.
System による Unit 起動順序 †
- Systemd は、default.target が依存している Unit を葉の方から芋づる式に実行する
- 通常 default.target は、シンボリックリンクになっており、シンボリックリンクの切り替えで initd の runlevel 切り替え相当のことができる
- たとえば

- default.target から一番遠いところから実行される
Unit の書き方 †
- ex) sshd.service
[Unit]
Description=OpenSSH server daemon
After=syslog.target network.target auditd.service
[Service]
ExecStartPre=/usr/bin/ssh-keygen -A
ExecStart=/usr/sbin/sshd -D -e
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
設定項目 | 設定内容 | | 設定値 | 設定の意味 |
Unit File | [Unit] | Description | Unitの説明 |
Documentation | DocumentのURL |
Requires | このUnitが必要とするUnit |
Wants | このUnitが必要とするUnit |
After | このUnitより先に起動するUnit (=依存関係がこのUnitよりも根から遠い(After)) |
Before | このUnitより後に起動するUnit (=依存関係がこのUnitよりも根に近い(Before)) |
[Install] | WantedBy? | enable時に、このUnitのsymlinkを $(WantedBy?}.wants/ に作成 |
RequiredBy? | enable時に、このUnitのsymlinkを $(RequiredBy?}.requires/ に作成 |
Also | enable/disable時に、このUnitに連動してenable/disableするUnit |
Alias | このUnitの別名。symlinkのファイル名 |
[Service] | ExecStart? | サービス起動コマンド |
ExecReload? | サービスリロードコマンド |
ExecStop? | サービス停止コマンド |
ExecStartPre? | サービス起動前処理。(先頭に"-"をつけると失敗しても起動シーケンスを続ける。たとえば、-/bin/rm /var/someapp/cache。cache がなくて rm コマンドが失敗しても起動シーケンスとしてはOK) |
ExecStartPost? | サービス起動後処理 |
ExecStopPost? | サービス停止後処理(異常停止後にも呼ばれる) |
EnvironmentFile? | 環境定義ファイル |
Type | 起動完了の判定方法 | ◯ | simple | デフォルト値。プロセス起動で正常起動と判定(そのまま常駐するタイプ) |
| forking | 起動したプロセスの終了で正常起動と判定(起動シェルタイプ) |
| oneshot | 何らかの処理をして終了するタイプのサービス |
| dbus | D-Bus(プロセス間通信)を利用するサービス |
PIDFile | PIDファイル。forkingタイプのサービスの場合、メインプロセスのPIDを記入したファイルを指定する必要がある。 simple、forking にかかわらず PID は $MAINPID で参照可能 |
BusName? | D-Busへのbus接続名 |
Restart | プロセス異常停止時の再起動 | ◯ | no | デフォルト値。しない |
| always | いつも |
| on-success | 終了コード0のとき |
| on-failure | 終了コード0以外のとき |
PrivateTmp? | このサービス専用の /tmp、/var/tmp を用意するか | ◯ | false | デフォルト値。用意しない |
| true | 用意する。(サービスからは /tmp、/var/tmp として見える、そのサービス固有の領域を確保する) |
systemctl コマンド †
service の状態確認 †
core@coreos1 /usr/lib/systemd/system $ systemctl status ntpd.service
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib64/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2015-02-04 12:23:33 UTC; 1h 19min ago
Main PID: 455 (ntpd)
CGroup: /system.slice/ntpd.service
└─455 /usr/sbin/ntpd -g -n -f /var/lib/ntp/ntp.drift -u ntp:ntp
Feb 04 12:23:33 coreos1 ntpd[455]: Listening on routing socket on fd #21 for interface updates
Feb 04 12:23:33 coreos1 ntpd[455]: 4 Feb 12:23:33 ntpd[455]: Listening on routing socket on fd #21 for i...dates
Feb 04 12:23:34 coreos1 ntpd[455]: bind(24) AF_INET6 fe80::20c:29ff:fe16:dd7e%2%2#123 flags 0x11 failed:...dress
Feb 04 12:23:34 coreos1 ntpd[455]: unable to create socket on ens192 (6) for fe80::20c:29ff:fe16:dd7e%2#123
Feb 04 12:23:34 coreos1 ntpd[455]: failed to init interface for address fe80::20c:29ff:fe16:dd7e%2
Feb 04 12:23:34 coreos1 ntpd[455]: 4 Feb 12:23:34 ntpd[455]: bind(24) AF_INET6 fe80::20c:29ff:fe16:dd7e%...dress
Feb 04 12:23:34 coreos1 ntpd[455]: 4 Feb 12:23:34 ntpd[455]: unable to create socket on ens192 (6) for f...2#123
Feb 04 12:23:34 coreos1 ntpd[455]: 4 Feb 12:23:34 ntpd[455]: failed to init interface for address fe80::...d7e%2
Feb 04 12:23:36 coreos1 ntpd[455]: Listen normally on 7 ens192 [fe80::20c:29ff:fe16:dd7e%2]:123
Feb 04 12:23:36 coreos1 ntpd[455]: 4 Feb 12:23:36 ntpd[455]: Listen normally on 7 ens192 [fe80::20c:29ff...]:123
Hint: Some lines were ellipsized, use -l to show in full.
基本的なステータスと、直近の syslog が表示される
service の 開始・停止・再起動 †
# systemctl start sshd.service
# systemctl stop sshd.service
# systemctl restart sshd.service
# systemctl reload sshd.service
- reload は、sshd.service ファイルを変更した場合に、sshd を再起動せずに変更内容を反映させるもの。Unit ファイルに ExecReload? が必要 (Unit ファイルに ExecReload? が無いのに systemctl reload を実行するとエラーになる)
- sshd.service
[Unit]
Description=OpenSSH server daemon
After=syslog.target network.target auditd.service
[Service]
ExecStartPre=/usr/bin/ssh-keygen -A
ExecStart=/usr/sbin/sshd -D -e
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
service が OS 起動時に起動するかの 有効・無効 †
- serivce を OS 起動時に起動したい場合には、Unit ファイルに WantedBy?=multi-user.target を記入する。GUI のサービスの場合は graphical.target
- 有効化 (multi-user.target.wants/ に service unit ファイルのシンボリックリンクを作る)
$ sudo systemctl enable ntpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib64/systemd/system/ntpd.service.
- 無効化 (multi-user.target.wants/ から service unit ファイルのシンボリックリンクを削除する)
$ sudo systemctl disable ntpd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/ntpd.service.
service の有効・無効の状態確認 †
core@coreos1 /usr/lib/systemd/system $ systemctl list-unit-files --type=service
UNIT FILE STATE
addon-config@.service static
addon-run@.service static
autovt@.service disabled
blk-availability.service disabled
clean-ca-certificates.service static
console-getty.service disabled
console-shell.service disabled
container-getty@.service static
coreos-setup-environment.service disabled
coreos-tmpfiles.service static
dbus-org.freedesktop.hostname1.service static
dbus-org.freedesktop.locale1.service static
dbus-org.freedesktop.login1.service static
dbus-org.freedesktop.machine1.service static
dbus-org.freedesktop.resolve1.service disabled
dbus-org.freedesktop.timedate1.service static
dbus.service static
debug-shell.service disabled
dhcpcd.service disabled
dm-event.service disabled
docker.service disabled
early-docker.service disabled
emergency.service static
etcd.service static
extend-filesystems.service static
flanneld.service static
fleet.service static
fstrim.service static
getty@.service disabled
git-daemon@.service static
initrd-cleanup.service static
initrd-parse-etc.service static
initrd-switch-root.service static
initrd-udevadm-cleanup-db.service static
ip6tables-restore.service disabled
default.service からの依存関係のある serivice の状態の確認 †
core@coreos1 /usr/lib/systemd/system $ systemctl list-units --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
dbus.service loaded active running D-Bus System Message Bus
etcd.service loaded active running etcd
fleet.service loaded active running fleet daemon
getty@tty1.service loaded active running Getty on tty1
kmod-static-nodes.service loaded active exited Create list of required static device no
locksmithd.service loaded activating auto-restart Cluster reboot manager
ntpd.service loaded active running Network Time Service
polkit.service loaded active running Authorization Manager
serial-getty@ttyS0.service loaded active running Serial Getty on ttyS0
sshd-keygen.service loaded active exited Generate sshd host keys
sshd@3-192.168.38.101:22-192.168.38.1:63155.service loaded active running OpenSSH per-connection server
system-cloudinit@usr-share-oem-cloud\x2dconfig.yml.service loaded active exited Load cloud-config from
systemd-journal-flush.service loaded active exited Flush Journal to Persistent Storage
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-networkd.service loaded active running Network Service
systemd-random-seed.service loaded active exited Load/Save Random Seed
systemd-resolved.service loaded active running Network Name Resolution
systemd-sysctl.service loaded active exited Apply Kernel Variables
systemd-tmpfiles-setup-dev.service loaded active exited Create Static Device Nodes in /dev
systemd-tmpfiles-setup.service loaded active exited Create Volatile Files and Directories
systemd-udev-settle.service loaded active exited udev Wait for Complete Device Initializa
systemd-udev-trigger.service loaded active exited udev Coldplug all Devices
systemd-udevd.service loaded active running udev Kernel Device Manager
systemd-update-utmp.service loaded active exited Update UTMP about System Boot/Shutdown
systemd-user-sessions.service loaded active exited Permit User Sessions
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
update-engine.service loaded active running Update Engine
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
28 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
- running : デーモンとして起動している
- exited : 起動時に行われた処理。終了している
default.service からの依存関係の調査 †
core@coreos1 /usr/lib/systemd/system $ systemctl list-dependencies
default.target
● ├─dbus.service
● ├─extend-filesystems.service
● ├─issuegen.service
● ├─locksmithd.service
● ├─motdgen.path
● ├─motdgen.service
● ├─motdgen.timer
● ├─ntpd.service
● ├─ssh-key-proc-cmdline.service
● ├─sshd-keygen.service
● ├─systemd-ask-password-wall.path
● ├─systemd-logind.service
● ├─systemd-networkd.service
● ├─systemd-resolved.service
● ├─systemd-user-sessions.service
● ├─update-engine-stub.timer
● ├─update-engine.service
● ├─basic.target
● │ ├─paths.target
● │ ├─slices.target
● │ │ ├─-.slice
● │ │ └─system.slice
● │ ├─sockets.target
● │ │ ├─dbus.socket
● │ │ ├─docker.socket
● │ │ ├─sshd.socket
● │ │ ├─systemd-initctl.socket
● │ │ ├─systemd-journald-audit.socket
● │ │ ├─systemd-journald-dev-log.socket
● │ │ ├─systemd-journald.socket
● │ │ ├─systemd-shutdownd.socket
● │ │ ├─systemd-udevd-control.socket
● │ │ └─systemd-udevd-kernel.socket
● │ ├─sysinit.target
● │ │ ├─clean-ca-certificates.service
● │ │ ├─coreos-tmpfiles.service
● │ │ ├─dev-hugepages.mount
● │ │ ├─dev-mqueue.mount
● │ │ ├─kmod-static-nodes.service
● │ │ ├─ldconfig.service
● │ │ ├─proc-sys-fs-binfmt_misc.automount
● │ │ ├─sys-fs-fuse-connections.mount
● │ │ ├─sys-kernel-config.mount
● │ │ ├─sys-kernel-debug.mount
● │ │ ├─systemd-ask-password-console.path
● │ │ ├─systemd-binfmt.service
● │ │ ├─systemd-journal-catalog-update.service
● │ │ ├─systemd-journal-flush.service
● │ │ ├─systemd-journald.service
● │ │ ├─systemd-machine-id-commit.service
● │ │ ├─systemd-modules-load.service
● │ │ ├─systemd-random-seed.service
● │ │ ├─systemd-sysctl.service
● │ │ ├─systemd-sysusers.service
● │ │ ├─systemd-tmpfiles-setup-dev.service
● │ │ ├─systemd-tmpfiles-setup.service
● │ │ ├─systemd-udev-hwdb-update.service
● │ │ ├─systemd-udev-trigger.service
● │ │ ├─systemd-udevd.service
● │ │ ├─systemd-update-done.service
● │ │ ├─systemd-update-utmp.service
● │ │ ├─systemd-vconsole-setup.service
● │ │ ├─update-ca-certificates.service
● │ │ ├─xenserver-pv-version.service
● │ │ ├─cryptsetup.target
● │ │ ├─local-fs.target
● │ │ │ ├─lvm2-activation-early.service
● │ │ │ ├─lvm2-activation.service
● │ │ │ ├─media.mount
● │ │ │ ├─proc-xen.mount
● │ │ │ ├─remount-root.service
● │ │ │ ├─systemd-remount-fs.service
● │ │ │ ├─tmp.mount
● │ │ │ └─usr-share-oem.mount
● │ │ └─swap.target
● │ └─timers.target
● │ └─systemd-tmpfiles-clean.timer
● ├─getty.target
● │ ├─getty@tty1.service
● │ └─serial-getty@ttyS0.service
● ├─remote-fs.target
● │ └─lvm2-activation-net.service
● ├─system-config.target
● │ ├─coreos-setup-environment.service
● │ └─system-cloudinit@usr-share-oem-cloud\x2dconfig.yml.service
● └─user-config.target
● ├─user-cloudinit-proc-cmdline.service
● ├─user-cloudinit@var-lib-coreos\x2dinstall-user_data.path
● ├─user-configdrive.path
● └─system-config.target
● ├─coreos-setup-environment.service
● └─system-cloudinit@usr-share-oem-cloud\x2dconfig.yml.service
Computer