Systemctlを使用してSystemdサービスを管理する方法

リナックス

Linuxオペレーティングシステムでスタートアップサービスを管理するための標準プロセスをSystemdしました。 Linuxシステムの起動時に実行するプログラムを制御するために使用されます。 これはシステムマネージャーであり、Linuxオペレーティングシステムの新しい標準になりました。 Systemdを使用すると、任意のプロセスを実行および管理するためのカスタムsystemdサービスを作成できます。 このチュートリアルでは、Linux上のsystemdでサービスを管理する方法を説明します。

前提条件

  • SystemdでLinuxを実行しているサーバー。ここではUbuntu20.04サーバーを使用します。
  • ルートパスワードがサーバーに設定されます。

Systemdを確認する

デフォルトでは、Systemdはすべての主要なオペレーティングシステムにプリインストールされています。 次のコマンドを使用して、インストールされているSystemdのバージョンを確認できます。

systemctl --version

次の出力が表示されます。

systemd 245 (245.4-4ubuntu3)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid

次のコマンドを使用して、Systemdの場所を見つけることができます。

whereis systemd

次の出力が得られるはずです。

systemd: /usr/bin/systemd /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz

systemctlの場所を見つけるには、次のコマンドを実行します。

whereis systemctl

次の出力が得られるはずです。

systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

次のコマンドを使用して、Systemdが実行されているかどうかを確認することもできます。

ps -eaf | grep systemd

次の出力が得られるはずです。

root         269       1  0 09:57 ?        00:00:01 /lib/systemd/systemd-journald
root         294       1  0 09:57 ?        00:00:00 /lib/systemd/systemd-udevd
systemd+     297       1  0 09:57 ?        00:00:00 /lib/systemd/systemd-networkd
message+     319       1  0 09:57 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root         329       1  0 09:57 ?        00:00:00 /lib/systemd/systemd-logind
systemd+     382       1  0 09:57 ?        00:00:00 /lib/systemd/systemd-resolved
root        1128       1  0 10:47 ?        00:00:00 /lib/systemd/systemd --user
root        1177    1152  0 10:48 pts/0    00:00:00 grep --color=auto systemd

次のコマンドを使用して、システムの起動時に各プロセスにかかる時間を分析することもできます。

systemd-analyze blame

次の出力が得られるはずです。

3.662s csm.service                         
1.899s apt-daily-upgrade.service           
1.798s dev-sda1.device                     
1.400s systemd-logind.service              
1.336s fstrim.service                      
1.207s systemd-networkd.service            
1.131s systemd-resolved.service            
 856ms man-db.service                      
 842ms systemd-journald.service            
 415ms e2scrub_reap.service                
 320ms keyboard-setup.service              
 295ms networkd-dispatcher.service         
 239ms systemd-udev-trigger.service 

次のコマンドを使用して、起動時にクリティカルチェーンを分析するには:

systemd-analyze critical-chain

次の出力が得られるはずです。

The time when unit became active or started is printed after the "@" character.
The time the unit took to start is printed after the "+" character.

graphical.target @4.948s
??multi-user.target @4.946s
  ??csm.service @1.282s +3.662s
    ??basic.target @1.277s
      ??sockets.target @1.276s
        ??dbus.socket @1.274s
          ??sysinit.target @1.257s
            ??systemd-update-utmp.service @1.247s +8ms
              ??systemd-tmpfiles-setup.service @1.228s +17ms
                ??systemd-journal-flush.service @1.201s +24ms
                  ??systemd-journald.service @356ms +842ms
                    ??systemd-journald.socket @313ms
                      ??system.slice @225ms
                        ??-.slice @225ms

Systemdでサービスを管理する

Systemctlは、systemdサービスを管理するためのより簡単な方法を提供します。

Apacheサービスを開始するには、次のコマンドを実行します。

systemctl start apache2

Apacheサービスを再起動するには、次のコマンドを実行します。

systemctl restart apache2

Apacheサービスを停止するには、次のコマンドを実行します。

systemctl stop apache2

Apacheサービスをリロードするには、次のコマンドを実行します。

systemctl reload apache2

システムの再起動後にApacheサービスを開始できるようにするには、次のコマンドを実行します。

systemctl enable apache2

システムの起動からApacheサービスを削除するには、次のコマンドを実行します。

systemctl disable apache2

Apacheサービスを強制終了するには、次のコマンドを実行します。

systemctl kill apache2

Apacheサービスをマスクするには、次のコマンドを実行します。

systemctl mask apache2

Apacheプロセスのマスクを解除するには、次のコマンドを実行します。

systemctl unmask apache2

Apacheサービスが有効になっているかどうかを確認するには、次のコマンドを実行します。

systemctl is-enabled apache2

Apacheサービスのステータスを確認するには、次のコマンドを実行します。

systemctl status apache2

サービスが実行されている場合は、次の出力が得られるはずです。

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-01-09 10:53:12 UTC; 26s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 1708 (apache2)
      Tasks: 55 (limit: 2353)
     Memory: 5.8M
     CGroup: /system.slice/apache2.service
             ??1708 /usr/sbin/apache2 -k start
             ??1709 /usr/sbin/apache2 -k start
             ??1710 /usr/sbin/apache2 -k start

Jan 09 10:53:12 ubuntu2004 systemd[1]: Starting The Apache HTTP Server...

systemdファイルの名前を変更または削除した後、変更を適用するにはファイルをリロードする必要があります。 systemdサービスファイルをリロードするには、次のコマンドを実行します。

systemctl daemon-reload

Systemdユニットファイルの管理

上記のコマンドは、単一のサービスを管理するのに役立ちます。 ただし、システムの現在の状態を見つけるために使用することはできません。 この情報を提供するのに役立つ他の多くのコマンドがあります。

使用可能なすべてのユニットのリストを表示するには、次のコマンドを実行します。

systemctl list-unit-files

次の出力に、すべてのユニットファイルのリストが表示されます。

UNIT FILE                              STATE           VENDOR PRESET
proc-sys-fs-binfmt_misc.automount      static          enabled      
-.mount                                generated       enabled      
dev-hugepages.mount                    static          enabled      
dev-mqueue.mount                       static          enabled      
proc-sys-fs-binfmt_misc.mount          disabled        enabled      
sys-fs-fuse-connections.mount          static          enabled      
sys-kernel-config.mount                static          enabled      
sys-kernel-debug.mount                 static          enabled      
sys-kernel-tracing.mount               static          enabled      
acpid.path                             enabled         enabled      
ntp-systemd-netif.path                 enabled         enabled      
systemd-ask-password-console.path      static          enabled      
systemd-ask-password-wall.path         static          enabled      
session-2.scope                        transient       enabled      
acpid.service                          disabled        enabled      
apt-daily-upgrade.service              static          enabled      
apt-daily.service                      static          enabled      
[email protected]                        enabled         enabled      
console-getty.service                  disabled        disabled     
console-setup.service                  enabled         enabled      
[email protected]               static          enabled      
cron.service                           enabled         enabled      
cryptdisks-early.service               masked          enabled      
cryptdisks.service                     masked          enabled      
csm.service                            enabled         enabled      
dbus-org.freedesktop.hostname1.service static          enabled      
dbus-org.freedesktop.locale1.service   static          enabled      

すべてのアクティブなユニットのリストを表示するには、次のコマンドを実行します。

systemctl list-units

次の出力が表示されます。

  UNIT                                                                                        LOAD   ACTIVE SUB       DESCRIPTION             >
  proc-sys-fs-binfmt_misc.automount                                                           loaded active waiting   Arbitrary Executable Fil>
  sys-devices-pci0000:00-0000:00:03.0-virtio0-net-eth0.device                                 loaded active plugged   Virtio network device   >
  sys-devices-pci0000:00-0000:00:04.0-virtio1-net-eth1.device                                 loaded active plugged   Virtio network device   >
  sys-devices-pci0000:00-0000:00:05.0-virtio2-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged   QEMU_HARDDISK 1         >
  sys-devices-pci0000:00-0000:00:05.0-virtio2-host2-target2:0:0-2:0:0:0-block-sda.device      loaded active plugged   QEMU_HARDDISK           >
  sys-devices-platform-serial8250-tty-ttyS1.device                                            loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS10.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS11.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS12.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS13.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS14.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS15.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS16.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS17.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS18.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS19.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS2.device                                            loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS20.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS21.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS22.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS23.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS24.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS25.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS26.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS27.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS28.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS29.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS3.device                                            loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS30.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS31.device                                           loaded active plugged   /sys/devices/platform/se>

失敗したすべてのユニットのリストを表示するには、次のコマンドを実行します。

systemctl --failed

有効なサービスと無効なサービスを含むすべてのサービスを一覧表示するには、次のコマンドを実行します。

systemctl list-unit-files --type=service

次の出力が表示されます。

UNIT FILE                              STATE           VENDOR PRESET
acpid.service                          disabled        enabled      
apache-htcacheclean.service            disabled        enabled      
[email protected]           disabled        enabled      
apache2.service                        enabled         enabled      
[email protected]                       disabled        enabled      
apt-daily-upgrade.service              static          enabled      
apt-daily.service                      static          enabled      
[email protected]                        enabled         enabled      
console-getty.service                  disabled        disabled     
console-setup.service                  enabled         enabled      
[email protected]               static          enabled      
cron.service                           enabled         enabled      
cryptdisks-early.service               masked          enabled      
cryptdisks.service                     masked          enabled      
csm.service                            enabled         enabled      
dbus-org.freedesktop.hostname1.service static          enabled      
dbus-org.freedesktop.locale1.service   static          enabled      
dbus-org.freedesktop.login1.service    static          enabled      
dbus-org.freedesktop.resolve1.service  enabled         enabled      
dbus-org.freedesktop.timedate1.service static          enabled      
dbus-org.freedesktop.timesync1.service masked          enabled      
dbus.service                           static          enabled      
debug-shell.service                    disabled        disabled     
dmesg.service                          enabled         enabled      
[email protected]                       static          enabled      
e2scrub_all.service                    static          enabled      
[email protected]                  static          enabled      
e2scrub_reap.service                   enabled         enabled      
emergency.service                      static          enabled      
fstrim.service                         static          enabled      
getty-static.service                   static          enabled      
[email protected]                         enabled         enabled      

SSHユニットのプロパティを確認するには、次のコマンドを実行します。

systemctl show sshd.service

次の出力が表示されます。

Id=sshd.service
Names=sshd.service
Requires=basic.target
Wants=system.slice
WantedBy=multi-user.target
Conflicts=shutdown.target
Before=shutdown.target multi-user.target
After=syslog.target network.target auditd.service systemd-journald.socket basic.target system.slice
Description=OpenSSH server daemon

Systemctlでソケットを管理する

Systemctlコマンドを使用してソケットを管理することもできます。

使用可能なすべてのシステムソケットを一覧表示するには、次のコマンドを実行します。

systemctl list-unit-files --type=socket

次の出力が表示されます。

UNIT FILE                        STATE    VENDOR PRESET
acpid.socket                     enabled  enabled      
dbus.socket                      static   enabled      
ssh.socket                       disabled enabled      
syslog.socket                    static   disabled     
systemd-fsckd.socket             static   enabled      
systemd-initctl.socket           static   enabled      
systemd-journald-audit.socket    static   enabled      
systemd-journald-dev-log.socket  static   enabled      
[email protected] static   enabled      
systemd-journald.socket          static   enabled      
[email protected]         static   enabled      
systemd-networkd.socket          disabled enabled      
systemd-rfkill.socket            static   enabled      
systemd-udevd-control.socket     static   enabled      
systemd-udevd-kernel.socket      static   enabled      

SSHソケットを開始するには、次のコマンドを実行します。

systemctl start ssh.socket

SSHソケットを停止するには、次のコマンドを実行します。

systemctl stop ssh.socket

SSHソケットを再起動するには、次のコマンドを実行します。

systemctl restart ssh.socket

SSHソケットのステータスを確認するには、次のコマンドを実行します。

systemctl status ssh.socket

起動時にSSHソケットを有効または無効にするには、次のコマンドを実行します。

systemctl enable ssh.socket
systemctl disable ssh.socket

Systemctl追加コマンド

systemctlコマンドを使用して、いくつかのタスクを実行できます。 システムをシングルユーザーモードにする場合は、次のコマンドを実行します。

systemctl rescue

システムを停止するには、次のコマンドを実行します。

systemctl halt

システムをシャットダウンするには、次のコマンドを実行します。

systemctl poweroff

システムを再起動するには、次のコマンドを実行します。

systemctl reboot

結論

上記のガイドでは、Linuxでsystemdサービスを管理および制御する方法を学習しました。 これがsystemdインスタンスとの対話と制御に役立つことを願っています。 ご不明な点がございましたら、お気軽にお問い合わせください。

Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です