linux-icon

Linux OS

【systemctl】systemdでサービスを管理する方法

systemdの概要

systemdを採用したシステムではsystemdプロセスが起動し、各種サービスを管理する。systemdでは、システムの起動処理は「Unit」と呼ばれる処理単位に分かれている。サービスを起動するUnitやファイルシステムをマウントするUnitなどがある。以下で紹介する「httpd.service」httpdサービスの起動に使われるUnit。

systemdの起動手順

システムが起動するとdefault.targetというUnitが処理される。Amazon Linux 2023の場合、/usr/lib/systemd/system以下にある。

$ ls -l /usr/lib/systemd/system/default.target
lrwxrwxrwx. 1 root root 16 Jan 17 22:52 /usr/lib/systemd/system/default.target -> graphical.target

systemctlコマンドの主なサブコマンド

サブコマンド説明
startサービスを起動する
stopサービスを終了する
statusサービスの稼働状況を表示する
is-activeサービスが稼働しているかを確認する
restartサービスを再起動する
reloadサービスの設定を再読み込む
enableシステム起動時にサービスを自動起動する
disableシステム起動時にサービスを自動起動しない
mask指定したUnitをマスクし手動で起動できないようにする
unmask指定したUnitのマスクを解除する
list-dependenciesUnitの依存関係を表示する
list-units起動しているすべてのUnitと状態を表示する
list-unit-filesすべてのUnitを表示する
rebootシステムを再起動する
poweroffシステムをシャットダウンする

実践

前提として、sudo dnf install httpd でApacheをインストールしたサーバでコマンドを確認していく。OSAmazon Linux 20233

systemctl start

サブコマンドに start を指定することで httpd.service が起動する

$ sudo systemctl start httpd

systemctl status

先ほど start した httpd.service の状態を確認する。サブコマンドに status を指定することで対象のサービスの稼働状況を確認することができる。

4行目で Active: active (running) となっているため、httpd.service は稼働しているということがわかる。

$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
     Active: active (running) since Sun 2024-06-02 14:16:38 UTC; 15s ago
       Docs: man:httpd.service(8)
   Main PID: 25964 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
      Tasks: 177 (limit: 1114)
     Memory: 13.0M
        CPU: 70ms
     CGroup: /system.slice/httpd.service
             ├─25964 /usr/sbin/httpd -DFOREGROUND
             ├─25965 /usr/sbin/httpd -DFOREGROUND
             ├─25966 /usr/sbin/httpd -DFOREGROUND
             ├─25967 /usr/sbin/httpd -DFOREGROUND
             └─25968 /usr/sbin/httpd -DFOREGROUND

systemctl is-active

statusと類似しているが、稼働の有無のみ確認したい場合はこちらがすっきりとするかもしれない。稼働していれば「active」、稼働していなければ「inactive」と表示される。

$ sudo systemctl is-active httpd
active

systemctl stop

start した httpd.service をしてみる。サブコマンドに stop を指定することでサービスを終了することができる。

$ sudo systemctl stop httpd

先ほど紹介した systemctl status で状態を確認すると、 Active: inactive (dead) となっており、 httpd.service は終了していることがわかる。

$ sudo systemctl status httpd
○ httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
     Active: inactive (dead)
       Docs: man:httpd.service(8)

Jun 02 14:16:38 ip-10-0-2-17.ap-northeast-1.compute.internal systemd[1]: Starting httpd.service - The Apache HTTP Server...
Jun 02 14:16:38 ip-10-0-2-17.ap-northeast-1.compute.internal systemd[1]: Started httpd.service - The Apache HTTP Server.
Jun 02 14:16:38 ip-10-0-2-17.ap-northeast-1.compute.internal httpd[25964]: Server configured, listening on: port 80
Jun 02 14:23:01 ip-10-0-2-17.ap-northeast-1.compute.internal systemd[1]: Stopping httpd.service - The Apache HTTP Server...
Jun 02 14:23:02 ip-10-0-2-17.ap-northeast-1.compute.internal systemd[1]: httpd.service: Deactivated successfully.
Jun 02 14:23:02 ip-10-0-2-17.ap-northeast-1.compute.internal systemd[1]: Stopped httpd.service - The Apache HTTP Server.

systemctl restart

サブコマンドに restart を使用することでサービスを再起動することができる。

$ sudo systemctl restart httpd

再起動後のサービスは稼働している状態。

$ sudo systemctl is-active httpd
active

systemctl enable

ただサービスを起動した状態だと、サーバが再起動する際にサービスが稼働していない状態になり、サーバが再起動した都度コマンドを実行しなければならない。大変面倒である。

サブコマンドに enable を使用すると、サーバの起動時に指定したサービスが自動起動するように設定することができる。

$ sudo systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

systemctl status の出力結果も少し変わっている。

enableを実行する前は、Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled) の「enabled」 が「disabled」になっていた。

$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
     Active: active (running) since Sun 2024-06-02 14:30:19 UTC; 6min ago
       Docs: man:httpd.service(8)
   Main PID: 26604 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
      Tasks: 177 (limit: 1114)
     Memory: 12.9M
        CPU: 287ms
     CGroup: /system.slice/httpd.service
             ├─26604 /usr/sbin/httpd -DFOREGROUND
             ├─26632 /usr/sbin/httpd -DFOREGROUND
             ├─26633 /usr/sbin/httpd -DFOREGROUND
             ├─26634 /usr/sbin/httpd -DFOREGROUND
             └─26635 /usr/sbin/httpd -DFOREGROUND

Jun 02 14:30:19 ip-10-0-2-17.ap-northeast-1.compute.internal systemd[1]: Starting httpd.service - The Apache HTTP Server...
Jun 02 14:30:19 ip-10-0-2-17.ap-northeast-1.compute.internal systemd[1]: Started httpd.service - The Apache HTTP Server.
Jun 02 14:30:19 ip-10-0-2-17.ap-northeast-1.compute.internal httpd[26604]: Server configured, listening on: port 80

systemctl disable

disableは、enableと逆でサーバの起動時にサービスが自動起動しないようにすることができる。

$ sudo systemctl disable httpd
Removed "/etc/systemd/system/multi-user.target.wants/httpd.service".

稼働状況を確認すると以下の出力(抜粋)

$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
     Active: active (running) since Sun 2024-06-02 14:30:19 UTC; 12min ago

systemctl list-units

起動しているすべてのUnitと状態を表示する

  $ sudo systemctl list-units -t service 
  UNIT                                                  LOAD   ACTIVE SUB     DESCRIPTION                                      
  acpid.service                                         loaded active running ACPI Event Daemon
  amazon-ssm-agent.service                              loaded active running amazon-ssm-agent
  atd.service                                           loaded active running Deferred execution scheduler
  auditd.service                                        loaded active running Security Auditing Service
  chronyd.service                                       loaded active running NTP client/server
  cloud-config.service                                  loaded active exited  Apply the settings specified in cloud-config
  cloud-final.service                                   loaded active exited  Execute cloud user/final scripts
  cloud-init-local.service                              loaded active exited  Initial cloud-init job (pre-networking)
  cloud-init.service                                    loaded active exited  Initial cloud-init job (metadata service crawler)
  dbus-broker.service                                   loaded active running D-Bus System Message Bus
  dracut-shutdown.service                               loaded active exited  Restore /run/initramfs on shutdown
  getty@tty1.service                                    loaded active running Getty on tty1
  gssproxy.service                                      loaded active running GSSAPI Proxy Daemon
  kmod-static-nodes.service                             loaded active exited  Create List of Static Device Nodes

systemctl list-unit-files

すべてのUnitを表示する

$ sudo systemctl list-unit-files -t service 

UNIT FILE                              STATE           PRESET  
acpid.service                          disabled        disabled
amazon-ssm-agent.service               enabled         enabled 
arp-ethers.service                     disabled        disabled
atd.service                            enabled         enabled 
auditd.service                         enabled         enabled 
auth-rpcgss-module.service             static          -       
autovt@.service                        alias           -       
cfn-hup.service                        disabled        disabled
chrony-config.service                  enabled         enabled 
chrony-wait.service                    disabled        disabled
chronyd.service                        enabled         enabled 
cloud-config.service                   enabled         disabled
cloud-final.service                    enabled         disabled
cloud-init-hotplugd.service            static          -       
cloud-init-local.service               enabled         disabled

systemctl list-units -t service -t service --state=enabled

このコマンドではシステム起動時に自動起動するサービスの一覧を表示できる(enable状態になっているサービス)

$ sudo systemctl list-unit-files -t service --state=enabled
UNIT FILE                            STATE   PRESET  
amazon-ssm-agent.service             enabled enabled 
atd.service                          enabled enabled 
auditd.service                       enabled enabled 
chrony-config.service                enabled enabled 
chronyd.service                      enabled enabled 
cloud-config.service                 enabled disabled
cloud-final.service                  enabled disabled
cloud-init-local.service             enabled disabled
cloud-init.service                   enabled disabled
dbus-broker.service                  enabled enabled 
getty@.service                       enabled enabled 
hibinit-agent.service                enabled enabled 
import-state.service                 enabled enabled 
irqbalance.service                   enabled enabled 
libstoragemgmt.service               enabled enabled 
nfs-convert.service                  enabled disabled
rngd.service                         enabled enabled 
rpmdb-rebuild.service                enabled enabled 
selinux-autorelabel-mark.service     enabled enabled 
sshd.service                         enabled enabled 
sssd.service                         enabled enabled 
sysstat.service                      enabled enabled 
systemd-boot-update.service          enabled enabled 
systemd-homed-activate.service       enabled disabled
systemd-homed.service                enabled enabled 
systemd-network-generator.service    enabled enabled 
systemd-networkd-wait-online.service enabled disabled
systemd-networkd.service             enabled enabled 
systemd-pstore.service               enabled enabled 
systemd-resolved.service             enabled enabled 
update-motd.service                  enabled enabled 

31 unit files listed.

-Linux, OS