CentOS 7 systemdコマンド

CentOS 7ではsystemdが導入され、サービスの管理が従来と大きく変わっている。

サービスの起動、終了など

起動            systemctl start
終了            systemctl stop
強制終了          systemctl kill -s 9
再起動           systemctl restart
設定反映          systemctl reload
状態表示          systemctl status
自動起動の有効化      systemctl enable
自動起動の無効化      systemctl disable
自動起動の状態表示     systemctl is-enabled
サービス一覧の表示     systemctl --type service
起動失敗のユニット表示  systemctl --failed
起動しているユニット一覧 systemctl
全ての状態表示      systemctl --all

起動失敗のユニット表示

[root@server1 ~]# systemctl --failed
UNIT            LOAD   ACTIVE SUB    DESCRIPTION
clamd.service   loaded failed failed SYSV: Clam AntiVirus Daemon is a TCP/IP or socket protocol serv
network.service loaded failed failed LSB: Bring up/down networking
rngd.service loaded failed failed Hardware RNG Entropy Gatherer 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.

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

*clamd.serviceとnetwork.serviceとrngd.serviceが起動失敗している。

clamd.serviceは、再起動後ソケットファイルが見つからないというエラーで clamd が起動失敗している。原因はcentos7では/ver/runがtempfsになったので再起動後にはディレクトリ等全て消えてしまう。なのでソケットファイルが見つからないというエラーになる。
対策は、再起動後にディレクトリーを作成するように設定ファイルを編集

[root@server1 ~]# vi /etc/init.d/clamd
start(){
        echo -n "Starting Clam AntiVirus Daemon: "
        #再起動後にclamavのdirを作成
        mkdir /var/run/clamav > /dev/null 2>&1
        chown clamav:clamav /var/run/clamav > /dev/null 2>&1
        daemon clamd
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/clamd
        return $RETVAL
}

2015/1/2追記
以前はclamdの設定ファイルを編集していたが、/etc/tmpfiles.d/にclamd.confを追記することで、OS起動時に/var/run/clamavディレクトリを作成できるように変更

[root@server1 ~]# vi /etc/tmpfiles.d/clamd.conf
d /var/run/clamav 0755 root root - ← 追加
[root@server1 ~]# systemd-tmpfiles --create clamd.conf

systemctlをリロード
[root@server1 ~]# systemctl daemon-reload
clamdを再起動
[root@server1 ~]# systemctl restart clamd

network.serviceは、InterfaceでIPアドレスを指定していると起動失敗するらしい。
Networkに関するユニットの自動起動の確認。

[root@server1 ~]# systemctl list-unit-files | grep Network
dbus-org.freedesktop.NetworkManager.service enabled
NetworkManager-dispatcher.service           enabled
NetworkManager-wait-online.service          disabled
NetworkManager.service                      enabled

NetworkManager-wait-online.serviceを自動起動設定
[root@server1 ~]# systemctl enable NetworkManager-wait-online.service
ln -s '/usr/lib/systemd/system/NetworkManager-wait-online.service' '/etc/systemd/system/multi-user.target.wants/NetworkManager-wait-online.service'
[root@server1 ~]# systemctl list-unit-files | grep Network
dbus-org.freedesktop.NetworkManager.service enabled
NetworkManager-dispatcher.service           enabled
NetworkManager-wait-online.service          enabled
NetworkManager.service                      enabled

networkを再起動
[root@server1 ~]# systemctl restart network

rngd.serviceは、多くのユーザーを抱え、乱数を使った認証サーバーなどで使うデーモン。自宅サーバーでは停止しても問題ないので自動起動も無効にする。

[root@server1 ~]# systemctl stop rngd.service
[root@server1 ~]# systemctl disable rngd.service

systemdをリロード
[root@server1 ~]# systemctl daemon-reload

サーバーを再起動

[root@server1 ~]# reboot

起動失敗のユニット確認

[root@server1 ~]# 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'.

起動失敗のユニットは無くなった。

ユニットの自動起動が有効、無効の一覧

[root@server1 ~]# systemctl list-unit-files -t service
UNIT FILE                                   STATE
abrt-ccpp.service                           enabled
abrt-oops.service                           enabled
abrt-pstoreoops.service                     disabled
abrt-vmcore.service                         enabled
abrt-xorg.service                           enabled
abrtd.service                               enabled
accounts-daemon.service                     enabled
alsa-restore.service                        static
alsa-state.service                          static
alsa-store.service                          static
anaconda-direct.service                     static
anaconda-noshell.service                    static
anaconda-shell@.service                     static
anaconda-sshd.service                       static
anaconda-tmux@.service                      static
anaconda.service                            static
arp-ethers.service                          disabled
atd.service                                 enabled
auditd.service                              enabled
autofs.service                              disabled
autovt@.service                             disabled
avahi-daemon.service                        enabled
blk-availability.service                    disabled
bluetooth.service                           enabled
brandbot.service                            static
brltty.service                              disabled
canberra-system-bootup.service              disabled
canberra-system-shutdown-reboot.service     disabled
canberra-system-shutdown.service            disabled
certmonger.service                          disabled
cgconfig.service                            disabled
cgdcbxd.service                             disabled
cgred.service                               disabled
chrony-wait.service                         disabled
chronyd.service                             enabled
colord.service                              static
configure-printer@.service                  static
console-getty.service                       disabled
console-shell.service                       disabled
cpupower.service                            disabled
crond.service                               enabled
cups-browsed.service                        disabled
cups.service                                enabled
dbus-org.bluez.service                      enabled
dbus-org.freedesktop.Avahi.service          enabled
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.ModemManager1.service  enabled
dbus-org.freedesktop.NetworkManager.service enabled
dbus-org.freedesktop.nm-dispatcher.service  enabled
dbus-org.freedesktop.timedate1.service      static
dbus.service                                static
debug-shell.service                         disabled
display-manager.service                     enabled
dm-event.service                            disabled
dmraid-activation.service                   enabled
dnsmasq.service                             disabled
dovecot.service                             enabled
dracut-cmdline.service                      static
dracut-initqueue.service                    static
dracut-mount.service                        static
dracut-pre-mount.service                    static
dracut-pre-pivot.service                    static
dracut-pre-trigger.service                  static
dracut-pre-udev.service                     static
dracut-shutdown.service                     static
ebtables.service                            disabled
emergency.service                           static
fcoe.service                                disabled
firewalld.service                           disabled
firstboot-graphical.service                 enabled
fprintd.service                             static
gdm.service                                 enabled
getty@.service                              enabled
gssproxy.service                            disabled
halt-local.service                          static
htcacheclean.service                        static
httpd.service                               enabled
hypervkvpd.service                          enabled
hypervvssd.service                          enabled
initial-setup-graphical.service             disabled
initial-setup-text.service                  disabled
initrd-cleanup.service                      static
initrd-parse-etc.service                    static
initrd-switch-root.service                  static
initrd-udevadm-cleanup-db.service           static
instperf.service                            static
ip6tables.service                           disabled
ipsec.service                               disabled
iptables.service                            disabled
irqbalance.service                          enabled
iscsi.service                               enabled
iscsid.service                              disabled
iscsiuio.service                            disabled
kdump.service                               disabled
kmod-static-nodes.service                   static
ksm.service                                 enabled
ksmtuned.service                            enabled
libstoragemgmt.service                      enabled
libvirt-guests.service                      disabled
libvirtd.service                            enabled
lldpad.service                              disabled
lvm2-lvmetad.service                        disabled
lvm2-monitor.service                        enabled
lvm2-pvscan@.service                        static
mariadb.service                             enabled
mdmon@.service                              static
mdmonitor.service                           enabled
messagebus.service                          static
microcode.service                           enabled
ModemManager.service                        enabled
multipathd.service                          enabled
netcf-transaction.service                   disabled
NetworkManager-dispatcher.service           enabled
NetworkManager-wait-online.service          enabled
NetworkManager.service                      enabled
nfs-blkmap.service                          disabled
nfs-idmap.service                           disabled
nfs-lock.service                            enabled
nfs-mountd.service                          disabled
nfs-rquotad.service                         disabled
nfs-secure-server.service                   disabled
nfs-secure.service                          disabled
nfs-server.service                          disabled
nfs.service                                 disabled
nfslock.service                             disabled
nmb.service                                 enabled
ntpd.service                                enabled
ntpdate.service                             disabled
numad.service                               disabled
oddjobd.service                             disabled
packagekit-offline-update.service           enabled
plymouth-halt.service                       disabled
plymouth-kexec.service                      disabled
plymouth-poweroff.service                   disabled
plymouth-quit-wait.service                  disabled
plymouth-quit.service                       disabled
plymouth-read-write.service                 disabled
plymouth-reboot.service                     disabled
plymouth-start.service                      disabled
plymouth-switch-root.service                static
polkit.service                              static
postfix.service                             enabled
psacct.service                              disabled
qemu-guest-agent.service                    static
quotaon.service                             static
radvd.service                               disabled
rc-local.service                            static
rdisc.service                               disabled
realmd.service                              static
rescue.service                              static
rhel-autorelabel-mark.service               static
rhel-autorelabel.service                    static
rhel-configure.service                      static
rhel-dmesg.service                          disabled
rhel-domainname.service                     disabled
rhel-import-state.service                   static
rhel-loadmodules.service                    static
rhel-readonly.service                       static
rngd.service                                disabled
rpcbind.service                             enabled
rpcgssd.service                             disabled
rpcidmapd.service                           disabled
rpcsvcgssd.service                          disabled
rsyncd.service                              disabled
rsyslog.service                             enabled
rtkit-daemon.service                        enabled
saslauthd.service                           enabled
serial-getty@.service                       disabled
smartd.service                              enabled
smb.service                                 enabled
speech-dispatcherd.service                  disabled
spice-vdagentd.service                      enabled
sshd-keygen.service                         static
sshd.service                                enabled
sshd@.service                               static
sssd.service                                disabled
svnserve.service                            disabled
sysstat.service                             enabled
systemd-ask-password-console.service        static
systemd-ask-password-plymouth.service       static
systemd-ask-password-wall.service           static
systemd-backlight@.service                  static
systemd-binfmt.service                      static
systemd-fsck-root.service                   static
systemd-fsck@.service                       static
systemd-halt.service                        static
systemd-hibernate.service                   static
systemd-hostnamed.service                   static
systemd-hybrid-sleep.service                static
systemd-initctl.service                     static
systemd-journal-flush.service               static
systemd-journald.service                    static
systemd-kexec.service                       static
systemd-localed.service                     static
systemd-logind.service                      static
systemd-machined.service                    static
systemd-modules-load.service                static
systemd-nspawn@.service                     disabled
systemd-poweroff.service                    static
systemd-quotacheck.service                  static
systemd-random-seed.service                 static
systemd-readahead-collect.service           enabled
systemd-readahead-done.service              static
systemd-readahead-drop.service              enabled
systemd-readahead-replay.service            enabled
systemd-reboot.service                      static
systemd-remount-fs.service                  static
systemd-shutdownd.service                   static
systemd-suspend.service                     static
systemd-sysctl.service                      static
systemd-timedated.service                   static
systemd-tmpfiles-clean.service              static
systemd-tmpfiles-setup-dev.service          static
systemd-tmpfiles-setup.service              static
systemd-udev-settle.service                 static
systemd-udev-trigger.service                static
systemd-udevd.service                       static
systemd-update-utmp-runlevel.service        static
systemd-update-utmp.service                 static
systemd-user-sessions.service               static
systemd-vconsole-setup.service              static
target.service                              disabled
teamd@.service                              static
tuned.service                               enabled
udisks2.service                             static
upower.service                              disabled
usbmuxd.service                             static
virtlockd.service                           static
vmtoolsd.service                            enabled
vsftpd.service                              enabled
vsftpd@.service                             disabled
wacom-inputattach@.service                  static
wpa_supplicant.service                      disabled
yum-cron.service                            enabled

237 unit files listed.

enableとなっているところは自動起動が有効、disabledとなっているところは自動起動が無効という意味。staticは他のユニットに依存するもので、これ単体で有効/無効が設定できないもの。