systemd管理實踐指南nginx
CentOS 7 使用systemd替換了SysV。Systemd目的是要取代Unix時代以來一直在使用的init系統,兼容SysV和LSB的啓動腳本,並且夠在進程啓動過程當中更有效地引導加載服務。sql
systemctl命令是系統服務管理器指令,它實際上將 service 和 chkconfig 這兩個命令組合到一塊兒。apache
Systemd :系統啓動和服務器守護進程管理器,負責在系統啓動或運行時,激活系統資源,服務器進程和其它進程vim
Systemd 新特性:bash
系統引導時實現服務並行啓動
按需啓動守護進程
自動化 的 服務 依賴 關係管理
同時 採用socket 式與D-Bus 總線式激活 服務
系統狀態快照
unit 表示不一樣類型的systemd 對象,經過配置文件進行標識和配置;文件中主要包含了系統服務、監聽socket 、保存的系統快照以及其它與init服務器
配置文件:app
/usr/lib/systemd/system: 每一個 服務最主要的啓動腳本設置 ,相似於以前的/etc/init.d/
/run/systemd/system :系統執行過程當中所產生的服務腳本,比上面目錄優先運行
/etc/systemd/system :管理員創建的執行腳本,相似於 於/etc/rc.d/rcN.d/Sxx 類的功能,比上面目錄優先運行
Systemctl –t help 查看unit 類型less
[root@localhost ~]# systemctl -t help
Available unit types:
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope
[root@localhost ~]# ll /usr/lib/systemd/system | head -5
total 1220
-rw-r--r--. 1 root root 275 Dec 1 2015 abrt-ccpp.service
-rw-r--r--. 1 root root 380 Dec 1 2015 abrtd.service
-rw-r--r--. 1 root root 361 Dec 1 2015 abrt-oops.service
-rw-r--r--. 1 root root 266 Dec 1 2015 abrt-pstoreoops.service
Service unit
: 文件擴展名爲.service, 用於定義系統服務 Target unit
: 文件擴展名爲.target ,用於模擬實現「運行級別」 Device unit: .device
, 用於定義內核識別的設備 Mount unit: .mount
, 定義文件系統掛載點 Socket unit: .socket
, 用於標識進程間通訊用的socket 文件,也可在系統啓動時,延遲啓動服務,實現按需啓動 Snapshot unit: .snapshot
, 管理系統快照 Swap unit: .swap
, 用於標識swap 設備 Automount unit: .automount
,文件系統的自動掛載點 Path unit: .path
,用於定義文件系統中的一個文件或目錄使用,常 經常使用於當文件系統變化時,延遲激活服務,如:spool 目錄ssh
關鍵特性:socket
基於socket 的激活機制:socket 與服務程序分離
基於d-bus 的激活機制:
基於device 的激活機制:
基於path 的激活機制:
系統快照:保存各unit 的當前狀態信息於持久存儲設備中向後兼容sysv init 腳本
不兼容:
systemctl 命令固定不變,不可擴展
非由systemd 啓動的服務,systemctl 沒法與之通訊和控
命令格式:systemctl COMMAND name.service
注意:如下name.service表示某個具體服務
啓動:service name start ==> systemctl start name.service
中止:service name stop ==> systemctl stop name.service
重啓:service name restart ==> systemctl restart name.service
狀態:service name status ==> systemctl status name.service
條件式重啓:已啓動才重啓,不然不作操做: service name cond restart ==> systemctl try-restart name.service
重載或重啓服務:先加載,再啓動: systemctl reload-or-restart name.service
重載或條件式重啓服務:systemctl reload-or-try-restart name.service
禁止自動和手動啓動:systemctl mask name.service
取消禁止:systemctl unmask name.service
##查看 某服務當前激活與否的狀態: systemctl is-active name.service
##查看全部已經激活的服務: systemctl list-units --type|-t service
##查看全部服務: systemctl list-units --type service -a
systemctl list-units --type service --all 顯示狀態
loaded:Unit 配置文件已處理
active(running): 一次或屢次持續處理的運行
active(exited): 成功完成一次性的配置
active(waiting): 運行中,等待一個事件
inactive: 不運行
enabled: 開機啓動
disabled: 開機不啓動
static:開機不啓動,但可被另外一個啓用的服務激活
殺掉進程:
systemctl kill 進程名
設定某服務開機自啓:
chkconfig name on ==> systemctl enable name.service
設定某服務開機禁止啓動:
chkconfig name off ==> systemctl disable name.service
查看全部服務的開機自啓狀態:
chkconfig --list ==> systemctl list-unit-files --type service
用來列出該服務在哪些運行級別下啓用和 禁用
chkconfig sshd –list ==> ls /etc/systemd/system/*.wants/sshd.service
[root@localhost ~]# ls /etc/systemd/system/*.wants/sshd.service
/etc/systemd/system/multi-user.target.wants/sshd.service
查看服務是否開機自啓:
systemctl is-enabled name.service
[root@localhost ~]# systemctl is-enabled sshd.service
enabled
其它命令:
查看服務的依賴關係:
systemctl list-dependencies name.service
target units:
unit 配置文件:.target
ls /usr/lib/systemd/system/*.target
systemctl list-unit-files --type target --all //查看級別
運行級別:
0 ==> runlevel0.target, poweroff.target
1 ==> runlevel1.target, rescue.target
2 ==> runlevel2.target, multi-user.target
3 ==> runlevel3.target, multi-user.target
4 ==> runlevel4.target, multi-user.target
5 ==> runlevel5.target, graphical.target
6 ==> runlevel6.target, reboot.target
查看依賴性:
systemctl list-dependencies graphical.target //查看級別的依賴性
級別切換
級別切換:init N ==> systemctl isolate name.target
systemctl isolate multi-user.target //切換到3級別
注: 只有/lib/systemd/system/*.target 文件中AllowIsolate=yes 才能切換( 修改文件需執行systemctl daemon-reload 才能生效)
獲取默認運行級別
/etc/inittab ==> systemctl get-default
[root@localhost ~]# systemctl get-default
multi-user.target
修改默認級別
/etc/inittab ==> systemctl set-default name.target
systemctl set-default multi-user.target
ls –l /etc/systemd/system/default.target
#切換至緊急救援模式:
systemctl rescue
#切換至emergency 模式:
systemctl emergency
其它經常使用命令:
傳統命令init ,poweroff ,halt ,reboot 都成爲 systemctl 的軟連接
關機:systemctl halt 、systemctl poweroff
重啓:systemctl reboot
掛起:systemctl suspend
休眠: :systemctl hibernate
休眠並掛起:systemctl hybrid-sleep
/etc/systemd/system :系統管理員和用戶使用
/usr/lib/systemd/system :發行版打包者使用
實例:
[root@localhost ~]# cat /usr/lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
service unit file 文件一般由三部分組成:
[Unit] :定義與Unit 類型無關的通用選項;用於提供unit 的描述信息、unit 行爲及依賴關係等
[Service] :與特定類型相關的專用選項;此處爲Service 類型
[Install] :定義由「systemctl enable」 以及」systemctl disable「
Unit 段的經常使用選項:
Description :描述信息
After :定義unit 的啓動次序,表示當前unit 應該晚於哪些unit 啓動,其功能與Before 相反
Requires :依賴到的其它units ,強依賴,被依賴的units 沒法激活時,當前unit 即沒法激活
Wants :依賴到的其它units, , 弱依賴
Conflicts :定義units
Service 段的經常使用選項:
Type :定義影響ExecStart 及相關參數的功能的unit 進程啓動類型
simple :默認值,這個daemon 主要由ExecStart 接的指令串來啓動,啓動後常駐於內存中
forking :由ExecStart 啓動的程序透過spawns 延伸出其餘子程序來做爲此daemon 的主要服務。原生父程序在啓動結束後就會終止
oneshot :與simple 相似,不過這個程序在工做完畢後就結束了,不會常駐在內存中
dbus :與simple 相似,但這個daemon 必需要在取得一個D-Bus的 的名稱後,纔會繼續運做. 所以一般也要同時設定BusNname= 才行
notify :在啓動完成後會發送一個通知消息。還須要配合NotifyAccess 讓 來讓 Systemd 接收消息
idle :與simple 相似,要執行這個daemon 必需要全部的工做都順利執行完畢後纔會執行。這類的daemon 一般是開機到最後才執行便可的服務
EnvironmentFile :環境配置文件
ExecStart :指明啓動unit 要運行命令或腳本的絕對路徑
ExecStartPre: : ExecStart 前運行
ExecStartPost: : ExecStart 後運行
ExecStop :指明中止unit 要運行的命令或腳本
Restart :當設定Restart=1 時,則當次daemon 服務意外終止後,會再次自動啓動此服務
Install 段的經常使用選項:
Alias :別名,可以使用systemctl command Alias.service
RequiredBy :被哪些units 所依賴,強依賴
WantedBy :被哪些units 所依賴,弱依賴
Also :安裝本服務的時候還要安裝別的相關服務
注意:對於新建立的unit 文件,或者修改了的unit 文件,要通知systemd 重載此配置文件, 然後能夠選擇重啓 systemctl daemon-reload
(1)建立一個腳本,用於被建立的服務調用
[root@localhost system]# cat /testdir/bak.sh
#!/bin/bash
# 備份/etc/目錄
tar -Jcvf /testdir/etc-`date +%F`.tar.xz /etc/ &> dev/null
(2)給bak.sh腳本添加執行權限
[root@localhost ~]# chmod u+x /testdir/bak.sh
(3)建立bak.service服務
[root@localhost ~]# vim /etc/systemd/system/bak.service
[Unit]
Description=backup my etc
Requires=atd.service
[Service]
Type=simple
ExecStart=/bin/bash -c "echo /testdir/bak.sh|at now"
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start bak
(4)啓用服務
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl start bak
(5)驗證
[root@localhost system]# ll /testdir/
total 8132
-rwxr--r-- 1 root root 91 Sep 21 19:14 bak.sh
-rw-r--r-- 1 root root 4546560 Sep 21 19:15 etc-2016-09-21.tar.xz