Linux-admin-systemd-services-start-and-stop
Systemdサービスの開始と停止
*_systemd_* は、Linuxでサービスを実行する新しい方法です。 _systemd_には、_sysvinit_が優先されます。 _systemd_はLinuxの起動時間を短縮し、Linuxサービスを管理する標準的な方法になりました。 _systemd_は安定していますが、まだ進化しています。
initシステムとしての_systemd_は、Linuxカーネルの起動後にステータスの変更が必要なサービスとデーモンの両方を管理するために使用されます。 ステータスの変更により、サービス状態の開始、停止、リロード、および調整が適用されます。
まず、サーバーで現在実行されているsystemdのバージョンを確認しましょう。
[centos@localhost ~]$ systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP
+GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
[centos@localhost ~]$
CentOSバージョン7では、この執筆時点で完全に更新されたsystemdバージョン219が現在の安定バージョンです。
_systemd-analyze_を使用して、最後のサーバーの起動時間を分析することもできます
[centos@localhost ~]$ systemd-analyze
Startup finished in 1.580s (kernel) + 908ms (initrd) + 53.225s (userspace) = 55.713s
[centos@localhost ~]$
システムの起動時間が遅い場合は、_systemd-analyze blame_コマンドを使用できます。
[centos@localhost ~]$ systemd-analyze blame
40.882s kdump.service
5.775s NetworkManager-wait-online.service
4.701s plymouth-quit-wait.service
3.586s postfix.service
3.121s systemd-udev-settle.service
2.649s tuned.service
1.848s libvirtd.service
1.437s network.service
875ms packagekit.service
855ms gdm.service
514ms firewalld.service
438ms rsyslog.service
436ms udisks2.service
398ms sshd.service
360ms boot.mount
336ms polkit.service
321ms accounts-daemon.service
_systemd_を使用する場合、_units_の概念を理解することが重要です。 *ユニット*は、_systemd_が解釈方法を知っているリソースです。 ユニットは次のように12種類に分類されます-
- 。サービス
- 。ソケット
- 。デバイス
- 。マウント
- .automount
- 。スワップ
- 。ターゲット
- 。パス
- 。タイマー
- .snapshot
- 。スライス
- 。範囲
ほとんどの場合、ユニットサービスとして.serviceを使用します。 他のタイプについてさらに調査することをお勧めします。 systemd_サービスの開始と停止には.service_ユニットのみが適用されるため。
各_unit_は、次のいずれかにあるファイルで定義されます-
- /lib/systemd/system -ベースユニットファイル
- /etc/systemd/system -実行時に変更されたユニットファイル
systemctlを使用してサービスを管理する
_systemd_を使用するには、_systemctl_コマンドに精通する必要があります。 以下は、_systemctl_の最も一般的なコマンドラインスイッチです。
| Switch | Action |
|---|---|
| -t | Comma separated value of unit types such as service or socket |
| -a | Shows all loaded units |
| --state | Shows all units in a defined state, either: load, sub, active, inactive, etc.. |
| -H | Executes operation remotely. Specify Host name or host and user separated by @. |
基本的なsystemctlの使用法
systemctl [operation]
example: systemctl --state [servicename.service]
ボックスで実行されているすべてのサービスを簡単に確認します。
[root@localhost rdc]# systemctl -t service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-xorg.service loaded active running ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
accounts-daemon.service loaded active running Accounts Service
alsa-state.service loaded active running Manage Sound Card State (restore and store)
atd.service loaded active running Job spooling tools
auditd.service loaded active running Security Auditing Service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
blk-availability.service loaded active exited Availability of block devices
bluetooth.service loaded active running Bluetooth service
chronyd.service loaded active running NTP client/server
サービスを停止する
最初に、bluetoothサービスを停止します。
[root@localhost]# systemctl stop bluetooth
[root@localhost]# systemctl --all -t service | grep bluetooth
bluetooth.service loaded inactive dead Bluetooth service
[root@localhost]#
ご覧のとおり、bluetoothサービスは非アクティブになっています。
Bluetoothサービスを再度開始するには。
[root@localhost]# systemctl start bluetooth
[root@localhost]# systemctl --all -t service | grep bluetooth
bluetooth.service loaded active running Bluetooth service
[root@localhost]#
注-.service_が暗示されているため、bluetooth.serviceを指定しませんでした。 処理しているサービスに_unit type_を追加することを考えるのは良い習慣です。 したがって、ここからは、。service_拡張子を使用して、サービスユニットの操作に取り組んでいることを明確にします。
サービスで実行できる主なアクションは次のとおりです-
| Start | Starts the service |
| Stop | Stops a service |
| Reload | Reloads the active configuration of a service w/o stopping it (like kill -HUP in system v init) |
| Restart | Starts, then stops a service |
| Enable | Starts a service at boot time |
| Disable | Stops a service from automatically starting at run time |
上記のアクションは、主に次のシナリオで使用されます-
| Start | To bring a service up that has been put in the stopped state. |
| Stop | To temporarily shut down a service (for example when a service must be stopped to access files locked by the service, as when upgrading the service) |
| Reload | When a configuration file has been edited and we want to apply the new changes while not stopping the service. |
| Restart | In the same scenario as reload, but the service does not support reload. |
| Enable | When we want a disabled service to run at boot time. |
| Disable | Used primarily when there is a need to stop a service, but it starts on boot. |
サービスのステータスを確認するには-
[root@localhost]# systemctl status network.service
network.service - LSB: Bring up/down networking
Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
Active: active (exited) since Sat 2017-01-14 04:43:48 EST; 1min 31s ago
Docs: man:systemd-sysv-generator(8)
Process: 923 ExecStart =/etc/rc.d/init.d/network start (code=exited, status = 0/SUCCESS)
localhost.localdomain systemd[1]: Starting LSB: Bring up/down networking...
localhost.localdomain network[923]: Bringing up loopback interface: [ OK ]
localhost.localdomain systemd[1]: Started LSB: Bring up/down networking.
[root@localhost]#
_networking_サービスの現在のステータスを表示します。 私たちはネットワーキングに関連するすべてのサービスを見たい場合は、使用することができます-
[root@localhost]# systemctl --all -t service | grep -i network
network.service loaded active exited LSB: Bring up/
NetworkManager-wait-online.service loaded active exited Network Manager
NetworkManager.service loaded active running Network Manager
ntpd.service loaded inactive dead Network Time
rhel-import-state.service loaded active exited Import network
[root@localhost]#
サービスを管理する sysinit メソッドに精通している人にとっては、_systemd_に移行することが重要です。 _systemd_は、Linuxでデーモンサービスを開始および停止する新しい方法です。