CentOS7の systemd についてメモ。今回は systemd を用いてサービスの状態について表示したり、操作したり行いましょう。

システムコマンドで起動確認。

  • systemctl -t service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@localhost vagrant]# systemctl -t service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
firewalld.service loaded active running firewalld - dynamic firewall daemon
getty@tty1.service loaded active running Getty on tty1
kmod-static-nodes.service loaded active exited Create list of required static device nodes for the current kernel
lvm2-lvmetad.service loaded active running LVM2 metadata daemon
lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling
lvm2-pvscan@8:2.service loaded active exited LVM2 PV scan on device 8:2
network.service loaded failed failed LSB: Bring up/down networking
(省略)
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 Reboot/Shutdown
systemd-user-sessions.service loaded active exited Permit User Sessions
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
tuned.service loaded active running Dynamic System Tuning Daemon
vboxadd-service.service loaded active running LSB: VirtualBox Additions service
vboxadd-x11.service loaded active exited LSB: VirtualBox Linux Additions kernel modules
vboxadd.service loaded active exited LSB: VirtualBox Linux Additions kernel modules

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.

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

起動していないサービスも表示

  • systemctl -t service -all
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost vagrant]# systemctl -t service -all
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
brandbot.service loaded inactive dead Flexible Branding Service
cpupower.service loaded inactive dead Configure CPU power related settings
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
display-manager.service not-found inactive dead display-manager.service
dm-event.service loaded inactive dead Device-mapper event daemon
dracut-shutdown.service loaded inactive dead Restore /run/initramfs
ebtables.service loaded inactive dead Ethernet Bridge Filtering tables
emergency.service loaded inactive dead Emergency Shell
exim.service not-found inactive dead exim.service
firewalld.service loaded active running firewalld - dynamic firewall daemon
(省略)
sshd.service loaded active running OpenSSH server daemon
syslog.service not-found inactive dead syslog.service
systemd-ask-password-console.service loaded inactive dead Dispatch Password Requests to Console
systemd-ask-password-wall.service loaded inactive dead Forward Password Requests to Wall
systemd-binfmt.service loaded inactive dead Set Up Additional Binary Formats
systemd-fsck-root.service loaded inactive dead File System Check on Root Device
systemd-initctl.service loaded inactive dead /dev/initctl Compatibility Daemon
systemd-journal-flush.service loaded inactive dead Trigger Flushing of Journal to Persistent Storage

定義されているUNITの設定を確認することができる。

  • systemctl list-unit-files --type=service
1
2
3
4
5
6
7
8
9
10
11
[root@localhost vagrant]# systemctl list-unit-files --type=service
UNIT FILE STATE
auditd.service enabled
auth-rpcgss-module.service static
autovt@.service disabled
blk-availability.service disabled
brandbot.service static
console-getty.service disabled
console-shell.service disabled
cpupower.service disabled
crond.service enabled
  • enable - 有効
  • disabled - 無効
  • static - 自動起動設定を持たない

UNITを有効化・無効化する。

  • 無効化 systemctl disable UNIT-name

    1
    2
    3
    4
    5
    6
    7
    [root@localhost vagrant]# systemctl list-unit-files --type=service | grep send
    sendmail.service enabled
    [root@localhost vagrant]# systemctl disable sendmail
    rm '/etc/systemd/system/multi-user.target.wants/sendmail.service'
    rm '/etc/systemd/system/multi-user.target.wants/sm-client.service'
    [root@localhost vagrant]# systemctl list-unit-files --type=service | grep sendmail
    sendmail.service disabled
  • 有効化 systemctl enable UNIT-name

    1
    2
    3
    4
    5
    6
    7
    ot@localhost vagrant]# systemctl list-unit-files --type=service | grep sendmail
    sendmail.service disabled
    [root@localhost vagrant]# systemctl enable sendmail
    ln -s '/usr/lib/systemd/system/sendmail.service' '/etc/systemd/system/multi-user.target.wants/sendmail.service'
    ln -s '/usr/lib/systemd/system/sm-client.service' '/etc/systemd/system/multi-user.target.wants/sm-client.service'
    [root@localhost vagrant]# systemctl list-unit-files --type=service | grep sendmail
    sendmail.service enabled

サービスの操作(起動・停止など)

  • サービス起動 systemctl start Unit-name

    1
    2
    3
    4
    5
    [root@localhost vagrant]# ps auxfw | grep -i [s]endmai
    [root@localhost vagrant]# systemctl start sendmail
    [root@localhost vagrant]# ps auxfw | grep -i [s]endmai
    root 1333 0.0 0.4 90208 2264 ? Ss 12:57 0:00 sendmail: accepting connections
    smmsp 1345 0.0 0.4 85648 1900 ? Ss 12:57 0:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
  • サービス停止 systemctl stop Unit-name

    1
    2
    3
    4
    5
    6
    [root@localhost vagrant]# ps auxfw | grep -i [s]endmai
    root 1333 0.0 0.4 90208 2264 ? Ss 12:57 0:00 sendmail: accepting connections
    smmsp 1345 0.0 0.4 85648 1900 ? Ss 12:57 0:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
    [root@localhost vagrant]# systemctl stop sendmail
    [root@localhost vagrant]# ps auxfw | grep -i [s]endmai
    [root@localhost vagrant]#
  • サービス再起動 systemctl restart Unit-name

  • サービス再読込 systemctl reload Unit-name

    • reload 設定がない場合はエラーになる。

      1
      2
      [root@localhost vagrant]# systemctl reload sendmail
      Failed to issue method call: Job type reload is not applicable for unit sendmail.service.

追記(2015/05/20)

UNITってなんですか?と@harasou さんにご質問を頂いたので回答。

UNITとは?

systemdが操作することができるサービス名のこと。
タイプとして、target, mount, service, device の4種類分かれている。

運用で主に、使うことになるのはserviceであろう。