前回までは、Red Hat Enterprise Linux 7(RHEL7)で新たに導入されたLinux起動プロセス「systemd」の動作原理、特にその基礎となる「Unit」の考え方や仕組みを説明した。今回からは、より実践的な内容として、コマンドによるサービス管理の方法、そして、Unitの設定方法について解説を進めていこう。

systemctlコマンドで現在有効なUnitを一覧

 systemdの操作の基本は、「systemctl」コマンドだ。引数なしでこのコマンドを実行すると、現在有効化されているUnitの一覧が表示される。前回説明したようにUnitにはいくつかのタイプがあるが、「--type」オプションで特定タイプのUnitのみを表示することができる。Unit名が長い場合は途中が「...」のように省略表示されるが、「--full」オプションを付けると省略せずに表示されるようになる。デフォルトでは、lessコマンドにパイプされるようになっているが、これが不要なら「--no-pager」オプションを指定する。

 図5は、現在有効化されている、serviceタイプのUnitを一覧表示した例である。従来バージョンのRHEL環境における、「起動中サービスの一覧」に当たる。何らかの理由で、本来有効化されているべきUnitが有効化されていない場合は、「ACTIVE」の列に「failed」と表示される。

# systemctl --type=service 
UNIT					LOAD	ACTIVE	SUB		DESCRIPTION
auditd.service				loaded	active	running	Security Auditing Service
avahi-daemon.service			loaded	active	running	Avahi mDNS/DNS-SD Stack
chronyd.service				loaded	active	running	NTP client/server
crond.service				loaded	active	running	Command Scheduler
dbus.service				loaded	active	running	D-Bus System Message Bus
...(中略)...
systemd-u...sessions.service		loaded	active	exited	Permit User Sessions
systemd-v...le-setup.service		loaded	active	exited	Setup Virtual Console
tuned.service				loaded	active	running	Dynamic System Tuning Daemon

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.

40 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
このマークで改行
図5●起動中サービスの一覧表示

 現在の状態とは関係なく、定義されているすべてのUnitを確認する際は、「list-unit-files」というサブコマンドを使う。特定のタイプのUnitのみを確認する際は、前述の「--type」オプションを使用する。 図6は、serviceタイプについて表示した例だ。「STATE」の列は、システム起動時の自動起動が有効化されているかどうかを示している。「enabled」「disabled」は、それぞれ、「有効」「無効」で、「static」は、自動起動の設定を持たないUnitになる。list-unit-filesの操作は、RHEL6までの環境でいえば、「chkconfig --list」コマンドでサービスの自動起動設定を確認する操作に相当する。

# systemctl list-unit-files --type=service 
UNIT FILE			STATE
auditd.service			enabled
autovt@.service			disabled
avahi-daemon.service		enabled
brandbot.service		static
chrony-wait.service		disabled
chronyd.service			enabled
...(中略)...
tuned.service			enabled
wpa_supplicant.service		disabled

124 unit files listed.
図6●サービスの自動起動設定を確認