そろそろCentOS7をさわっていくか systemctlコマンドを確認していく

typeでの一覧表示。下記はserviceでのコマンド例。
ACTIVE欄がfailedになっている場合は本来有効化される設定のUnitが動いてない場合。

[root@localhost ~]# 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
(中略)
systemd-vconsole-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.

37 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.


failedしたUnitを表示する

[root@localhost ~]# systemctl --failed
0 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.


現在の状態関係なく定義されてるUnitを確認する。
grepと合わせて設定状態の個別確認にも使える。
STATEの詳細はenable 有効、disable 無効、static 自動起動設定を持たないもの

[root@localhost ~]# systemctl list-unit-files --type=service
UNIT FILE                                   STATE
arp-ethers.service                          disabled
auditd.service                              enabled
(中略)
systemd-user-sessions.service               static
systemd-vconsole-setup.service              static
teamd@.service                              static
tomcat.service                              disabled
tuned.service                               enabled
wpa_supplicant.service                      disabled

125 unit files listed.
[root@localhost ~]# systemctl list-unit-files --type=service | grep tomcat
tomcat.service                              disabled


Unitの有効・無効化を行うには
systemctl enable Unit名
systemctl disable Unit名
で行います。
systemctl is-enabled Unit名 で有効・無効化の状態を確認します。
下記はtomcatの状態を確認して有効化後、確認。
そして、また無効化して確認した例です。

[root@localhost ~]# systemctl is-enabled tomcat
disabled
[root@localhost ~]# systemctl enable tomcat
ln -s '/usr/lib/systemd/system/tomcat.service' '/etc/systemd/system/multi-user.target.wants/tomcat.service'
[root@localhost ~]# systemctl is-enabled tomcat
enabled
[root@localhost ~]# systemctl disable tomcat
rm '/etc/systemd/system/multi-user.target.wants/tomcat.service'
[root@localhost ~]# systemctl is-enabled tomcat
disabled


OS起動後にUnitを手動制御
systemctl start Unit名
systemctl restart Unit名
systemctl reload Unit名
systemctl stop Unit名
下記はtomcatをstart,stopした例
※設定ファイルに記述がない場合はコマンドは発行出来ません。

[root@localhost ~]# systemctl start tomcat
[root@localhost ~]# ps -ef | grep java
tomcat    2286     1 19 13:49 ?        00:00:01 java -classpath /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat -Dcatalina.home=/usr/share/tomcat -Djava.endorsed.dirs= -Djava.io.tmpdir=/var/cache/tomcat/temp -Djava.util.logging.config.file=/usr/share/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager org.apache.catalina.startup.Bootstrap start
root      2306  2164  0 13:49 pts/0    00:00:00 grep --color=auto java
[root@localhost ~]# systemctl stop tomcat
[root@localhost ~]# ps -ef | grep java
root      2344  2164  0 13:49 pts/0    00:00:00 grep --color=auto java


systemdを再起動して追加や変更のあったUnitの設定を読み込み直す。

[root@localhost ~]# systemctl daemon-reload