CentOS 7之systemd學習筆記
阿新 • • 發佈:2018-02-26
systemd systemctl 一、systemd簡介
systemd是CentOS 7和RHEL 7的init程序, 擁有如下新特性:
- 系統引導時實現服務並行啟動
- 按需激活進程
- 系統狀態快照
- 基於依賴關系定義服務控制邏輯
二、systemd核心概念: unit
systemd將各種系統啟動和運行的相關對象表示為各種不同類型的unit, 並提供了處理不同unit之間依賴關系的能力. 使用配置文件進行標識和配置, 文件中主要包含系統服務、監聽socket、保存的系統快照以及其他的init相關的信息, 配置文件路徑如下:
- /usr/lib/systemd/system/
- /run/systemd/systemd/system/
- /etc/systemd/system/
2.1 unit的類型
unit主要有以下類型: unit類型 |
文件擴展名 | 作用 |
---|---|---|
Service unit | .service | 用於封裝一個後臺服務進程 |
Target unit | .target | 用於模擬實現“運行級別” |
Device unit | .device | 用於定義內核識別的設備 |
Mount unit | .mount | 用於定義文件系統掛載點 |
Socket unit | .socket | 用於標識進程間通信用的socket文件 |
Snapshot unit | .snapshot | 管理系統快照 |
Swap unit | .swap | 用於標識swap設備 |
Automount | .automount | 用於標識文件系統的自動掛載點 |
Path unit | .path | 用於定義文件系統中的一個文件或目錄 |
2.2 關鍵特性
systemd關鍵特性如下:
- 基於socket的激活機制: socket與服務程序分離
- 基於bus的激活機制
- 基於device的激活機制
- 基於path的激活機制
- 系統快照: 保存各unit的當前狀態信息於持久存儲設備中
- 向後兼容sysV init腳本
Note: init的命令和systemd的命令不完全兼容, systemctl命令固定不變, 非由systemd啟動的服務, systemctl無法與之通信
三、systemd管理系統服務命令: systemctl
systemctl - 管理系統服務, 能兼容早期的服務腳本
# 用法: systemctl COMMAND name[.service]
# 啟動服務: service name start ==> systemctl start name[.service]
[root@zabbix system]# systemctl start httpd
# 停止服務: service name stop ==> systemctl stop name[.service]
[root@zabbix system]# systemctl stop httpd
# 重啟服務: service name restart ==> systemctl restart name[.service]
[root@zabbix system]# systemctl restart httpd
# 狀態: service name status ==> systemctl status name[.service]
[root@zabbix system]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-02-26 10:30:40 CST; 23min ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 3870 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─3870 /usr/sbin/httpd -DFOREGROUND
├─3927 /usr/sbin/httpd -DFOREGROUND
├─3928 /usr/sbin/httpd -DFOREGROUND
├─3929 /usr/sbin/httpd -DFOREGROUND
├─3930 /usr/sbin/httpd -DFOREGROUND
└─3931 /usr/sbin/httpd -DFOREGROUND
Feb 26 10:30:40 zabbix.leistudy.com systemd[1]: Starting The Apache HTTP Server...
Feb 26 10:30:40 zabbix.leistudy.com systemd[1]: Started The Apache HTTP Server.
Feb 26 10:31:40 zabbix.leistudy.com systemd[1]: Reloaded The Apache HTTP Server.
Feb 26 10:32:20 zabbix.leistudy.com systemd[1]: Reloaded The Apache HTTP Server.
Feb 26 10:39:18 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
Feb 26 10:39:28 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
Feb 26 10:39:38 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
# 條件式重啟: service name condrestart ==> systemctl try-restart name[.service]
[root@zabbix system]# systemctl try-restart httpd
# 重載或重啟服務: systemctl reload-or-restart name[.service]
[root@zabbix system]# systemctl reload-or-restart httpd
# 重載或條件式重啟: systemctl reload-or-try-restart name[.service]
[root@zabbix system]# systemctl reload-or-try-restart httpd
# 禁止設定為開機啟動: systemctl mask name[.service]
[root@zabbix system]# systemctl mask httpd
Created symlink from /etc/systemd/system/httpd.service to /dev/null.
# 取消禁止設定為開機自啟: systemctl unmask name[.service]
[root@zabbix system]# systemctl unmask httpd
Removed symlink /etc/systemd/system/httpd.service.
# 查看某服務當前激活狀態: systemctl is-active name[.service]
[root@zabbix system]# systemctl is-active httpd
active
# 查看所有已經激活的服務: systemctl list-units --type service
[root@zabbix system]# systemctl list-units --type service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
chronyd.service loaded active running NTP client/server
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
...
# 查看所有服務: systemctl list-unit --type service --all
[root@zabbix system]# systemctl list-units --type service --all
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
brandbot.service loaded inactive dead Flexible Branding Service
chronyd.service loaded active running NTP client/server
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
...
# 設定某服務開機自啟動: chkconfig name on ==> systemctl enable name[.service]
[root@zabbix system]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
# 設定某服務禁止開機自啟動: chkconfig name off ==> systemctl disable name[.service]
[root@zabbix system]# systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
# 查看所有服務的開機自啟狀態: chkconfig --list ==> systemctl list-unit-files --type service
[root@zabbix system]# systemctl list-unit-files --type service
UNIT FILE STATE
arp-ethers.service disabled
auditd.service enabled
[email protected] enabled
blk-availability.service disabled
...
# 查看服務是否開機自啟: systemctl is-enabled name[.service]
[root@zabbix system]# systemctl is-enabled httpd
enabled
# 查看服務的依賴關系: systemctl list-dependencies name[.service]
[root@zabbix system]# systemctl list-dependencies httpd
httpd.service
● ├─-.mount
● ├─system.slice
● └─basic.target
● ├─microcode.service
● ├─rhel-autorelabel-mark.service
● ├─rhel-autorelabel.service
● ├─rhel-configure.service
...
- target unit
# 運行級別:
# level 0 ==> runlevel0.target, poweroff.target
# level 1 ==> runlevel1.target, rescue.target
# level 2 ==> runlevel2.target, multi-user.target
# level 3 ==> runlevel3.target, multi-user.target
# level 4 ==> runlevel4.target, multi-user.target
# level 5 ==> runlevel5.target, graphical.target
# level 6 ==> runlevel6.target, reboot.target
# 級別切換: init N ==> systemctl isolate name.service
[root@zabbix ~]# systemctl isolate multi-user.target
# 查看級別: runlevel ==> systemctl list-units --type target
[root@zabbix ~]# systemctl list-units --type target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
cryptsetup.target loaded active active Encrypted Volumes
getty.target loaded active active Login Prompts
local-fs-pre.target loaded active active Local File Systems (Pre)
local-fs.target loaded active active Local File Systems
multi-user.target loaded active active Multi-User System
network-online.target loaded active active Network is Online
network.target loaded active active Network
paths.target loaded active active Paths
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
timers.target loaded active active Timers
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.
15 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use ‘systemctl list-unit-files‘.
# 獲取默認運行級別: /etc/inittab ==> systemctl get-default
[root@zabbix ~]# systemctl get-default
multi-user.target
# 修改默認級別: /etc/inittab ==> systemctl set-default name[.target]
[root@zabbix ~]# systemctl set-default rescue.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/rescue.target.
# 切換至緊急救援模式: systemctl resuce
# 切換至emergency模式: systemctl emergency
- 其他常用命令
# 關機: systemctl halt, systemctl poweroff
# 重啟: systemctl reboot
# 掛起: systemctl suspend
# 快照
# 保存系統快照: systemctl hibernate
# 快照並掛起: systemctl hibrid-sleep
CentOS 7之systemd學習筆記