官方文檔: 地址html
systemd是RH7系列操做系統開始啓用新的系統和服務管理器。它被設計爲與sysv init腳本向後兼容,並提供了一些功能,例如在引導時並行啓動系統服務,按需激活守護程序或基於依賴關係的服務控制邏輯。linux
systemd中引入了system units的概念,在units其中封裝有關係統服務(service),偵聽套接字(socket),以及與init系統啓動相關信息redis
unit類型 | 文件後綴名 | 描述 Service unit | .service | 服務類 Target unit | .target | 一個unit服務組,用於模擬實現運行級別 Automount unit | .automount | 文件系統自動掛載點 Device unit | .device | 內核識別的設備文件 Mount unit | .mount | 文件系統掛載點 Path unit | .path | 文件或目錄 Scope unit | .scope | 外部建立的進程 Slice unit | .slice | A group of hierarchically organized units that manage system processes. Snapshot unit | .snapshot | 系統快照 Socket unit | .socket | 套接字 Swap unit | .swap | 標識swap設備 Timer unit | .timer | systemd的計時器
/usr/lib/systemd/system/ | RPM包安裝時分發的unit文件 /run/systemd/system/ | systemd運行時建立的文件 /etc/systemd/system/ | systemctl enable建立的unit文件
基於socket的激活機制: 在程序不可用時,socket會保持可訪問狀態,而且全部消息都排隊等待.實現了socket與服務程序分離,能夠爲服務啓動一個socket而不須要當即啓動程序,經過socket鏈接激活程序.並以此實現了服務的並行啓動.shell
基於bus的激活機制: 使用d-bus進行進程間通訊的系統服務能夠在客戶端應用程序第一次嘗試與它們進行通訊時按需啓動centos
基於device的激活機制: 使用d-bus進行進程間通訊的系統服務能夠在客戶端應用程序第一次嘗試與它們進行通訊時按需啓動.利用USB,CD等設備掛載時激活服務服務器
基於path的激活機制: 支持基於路徑的激活的系統服務能夠在特定文件或目錄更改其狀態時按需啓動.網絡
基於mount的激活機制: systemd監視和管理掛載點或自動掛載點激活服務ssh
並行啓動系統服務: 根據socket激活機制,只要服務所須要的偵聽套接字就位,systemd就能夠並行啓動系統服務.減小系統啓動時間socket
系統快照: 保存unit的當前狀態於持久設備中,必要時載入.例如重啓前保存unit狀態,重啓後不從新初始化服務直接使用保存的狀態.ide
激活邏輯: 在激活或關閉單元以前,systemd會計算它的依賴關係,建立一個臨時事務,並驗證這個事務是否一致.
若是事務不一致,systemd將自動嘗試糾正錯誤,並在報告錯誤以前從中刪除不重要的做業.
兼容sysv init: 支持sysv init風格的啓動腳本
systemd系統和服務管理器的設計主要是兼容sysv init和upstart.主要的兼容性變化包括:
systemd對運行級別的支持有限.對早期的0-6的運行級別概念,systemd是模擬實現的,並不能一一對應的實現,但並非全部的systemd目標均可以直接映射到運行級別,所以,runlevel這個命令可能會返回n來指示未知的運行級別.同時避免使用init3,5來切換運行級別.
systemctl子命令是預約義好的,不支持自定義命令.對於centos7之前編寫sysv init啓動腳本除了定義start,stop,status等命令之外,咱們還能夠在腳本中自定義其它的子命令.在systemd中不能實現
不能控制使用systemctl管理工具之外啓動的服務,由於使用systemctl啓動服務.systemctl會存儲服務的元信息來查詢和管理服務.因此經過命令行啓動的守護進程,systemd是沒法肯定服務狀態.
systemd會檢查服務運行狀態,因此在關機時只會中止正在運行的服務進程
systemd不會讀取任何標準輸入數據流
systemd不會從調用用戶及其會話繼承任何上下文(如主目錄和環境變量),因此編寫服務時都使用絕對路徑
5分鐘超時時間,任何服務若是沒有被正常執行都會有5分鐘超時時間限制.
在之前版本使用sysv init或upstart管理位於/etc/rc.d/init.d/目錄中的腳本.在centos7中被service unit取代.是用systemctl命令管理
1, 服務基本命令對比
service和systemctl
service | systemctl | service name start | systemctl start name.service | 啓動服務 service name stop | systemctl stop name.service | 中止服務 service name restart | systemctl restart name.service | 重啓服務(沒啓動的服務會啓動) service name condrestart | systemctl try-restart name.service | 只重啓正在運行的服務 service name reload | systemctl reload name.service | 重載配置文件 service name status | systemctl status name.service | 檢查服務狀態 service name status | systemctl is-active name.service | 檢查服務是否啓動 service --status-all | systemctl list-units --type service --all | 顯示全部的服務狀態
chkconfig和systemctl
chkconfig | systemctl | chkconfig name on | systemctl enable name.service | 啓用開機自啓服務 chkconfig name off | systemctl disable name.service | 停用自啓服務 chkconfig --list name | systemctl status name.service | 檢查服務狀態 chkconfig --list name | systemctl is-enabled name.service | 查看服務是否自啓 chkconfig --list | systemctl list-unit-files --type service | 查看全部服務 chkconfig --list | systemctl list-dependencies --after | 列出在指定服務以前啓動的服務.(依賴) chkconfig --list | systemctl list-dependencies --before | 列出在指定服務以後啓動的服務.(被依賴)
2, 服務狀態信息
Loaded | 關於服務是否已經加載的信息,文件的絕對路徑以及是否被啓用的註釋。 Active | 服務是否正在運行,而後是啓動時間信息 Main PID | 服務主進程pid Docs | 服務的幫助文檔(man) Status | 系統服務的額外信息 Process | 進程額外信息 CGroup | Control Groups額外信息
3, 經常使用命令(start,stop,這些經常使用命令就不說明了)
列出全部當前激活服務
systemctl list-units --type service
列出全部服務,不論是否激活(LOAD爲notfound應該是尚未安裝)
systemctl list-units --type service --all
列出可開機自啓的服務
systemctl list-unit-files --type service
從新加載服務: 一個服務設置爲開機啓動使用enable會將/usr/lib/systemd/system/name.service軟連接到/etc/systemd/system/.可是enable命令不會重寫已經存在的連接,因此當咱們修改了服務文件就須要從新加載
systemctl reenable name.service
禁用服務 mask會將 /etc/systemd/system/name.service軟連接到/dev/null.從而禁止服務啓用.反操做unmask
systemctl mask name.service
顯示服務屬性信息
systemctl show auditd
服務的依賴關係
systemctl list-dependencies chronyd.service
結束服務
systemctl kill name.service
在Centos7以前版本中,擁有0-6編號的一組運行級別表明特定的操做模式.在Centos7中由systemd的target取代,經過一系列依賴關係將其餘systemd units組合在一塊兒,來模擬一個運行級別的概念.在運行級別的基礎上target有更豐富更靈活的運行模式.
1,sysv和systemd 區別
Runlevel| Target Units | 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 | 重啓
sysv | systemd | runlevel | systemctl list-units --type target | 查看運行級別. init N | systemctl isolate name.target | 改變運行級別
2, 經常使用命令
獲取默認target
systemctl get-default
查看激活的target
systemctl list-units --type target
查看全部target
systemctl list-units --type target --all
修改默認target
systemctl set-default name.target
切換級別
systemctl isolate graphical.target
救援模式
systemctl rescue #會加載驅動
systemctl emergency #emergency模式不會加載驅動和系統服務
關機
systemctl halt systemctl poweroff
重啓
systemctl reboot
掛起
將系統狀態保存在內存中,並關閉大部分設備.(不推薦,容易斷電致使內存中數據丟失)
systemctl suspend
休眠
將系統狀態保存在硬盤中並關閉.下次啓動直接充保存的文件中讀取系統信息
systemctl hibenate
休眠並掛起
systemctl hybrid-sleep
除了在本地控制systemd系統和服務管理器以外,systemctl實用程序還容許您經過ssh協議與在遠程服務器上運行的systemd進行交互.須要使用openssh協議,而且遠程服務器上openssh server開啓
systemctl -H root@192.168.10.200 list-units --type service root@192.168.10.200's password: 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 ...
一般咱們須要作一些自定義服務或者是許多編譯軟件並無提供systemd的服務文件,就須要本身在/etc/systemd/system/目錄中建立,編寫unit文件.文件命名格式
unit_name.type_extension unit_name:爲服務名稱 type_extension: 爲unit類型包括,service,target,socket,device等等
同時unit文件容許爲服務補充一個目錄用於存放配置文件的符號連接,好比redis.service就能夠在/etc/systemd/system下建立redis.service.d/limit.conf.相似的目錄redis.service.wants和redis.service.requires
1, unit 文件結構
由三部分組成:
Description | 簡要說明 Documentation | 參考文檔列表 After | 當前服務啓動以前必需要知足的條件(一般是target,service,socket),Before相反,定義當前服務以後的動做 Requires | 其它依賴關係,要求列出的服務與當前服務一塊兒激活,若是requires沒啓動成功,當前服務也失敗 Wants | 比Requires更弱的依賴關係,wants不會影響當前服務的激活. Conflicts | 互斥依賴和requires相反.
Type | 啓動模式,配置影響execstart和服務進程相關選項 ExecStart | 服務啓動所須要執行的命令或者腳本,還有ExecStartPre和ExecStartPost來指定ExecStar以前和以後的命令執行.Type=oneshot時能夠指定多個,按順序執行. ExecStop | 服務中止執行的命令或腳本 ExecReload | 從新加載執行的命令或腳本 Restart | 重啓服務執行的命令或腳本 RemainAfterExit | 默認值是false.若是設置爲true即便全部進程退出,服務也被視爲活動.Type=oneshot時特別有用
Type的模式 simple | 默認值,以execstart開始的進程是服務的主要進程 forking | 從execstart開始的進程產生一個子進程,成爲服務的主進程.父進程在啓動完成時退出 oneshot | 和simple相似,可是在啓動後續服務後退出進程 dbus | 與simple相似,但在以前要獲取一個d-bus name notify | 與simple相似,可是在以前要獲取 sd_notify() 函數發送一個通知消息 idle | 服務二進制的實際執行被延遲直到全部做業完成,這避免了狀態輸出與服務的shell輸出的混淆
Alias | 爲服務設置除systemctl enable外可以使用的別名 RequiredBy | 依賴列表,當服務被設爲開機自啓所須要的依賴列表 WantedBy | 同上,弱依賴(一般設爲WantedBy=multi-user.target) Also | 指定一塊兒安裝或卸載的設備列表 DefaultInstance | 啓用默認實例
大多數系統服務和rpm包的默認unit文件都存儲在/usr/lib/systemd/system/,經過符號連接到/etc/systemd/system/目錄,在須要對啓動過程進程自定義修改是不建議直接修改源文件,官方建議是經過下面兩種方法進行自定義
1, 建立配置文件目錄/etc/systemd/system/unit.d/,在這個目錄裏作擴展配置和附加功能,具體步驟爲:
建立目錄
mkdir /etc/systemd/system/name.service.d/
建立擴展文件,必須爲.conf
touch /etc/systemd/system/name.service.d/config_name.conf
修改功能,建議只建立專一於一個任務的小配置文件。
[Service] ExecStartPost=/usr/local/bin/custom.sh
從新加載
systemctl daemon-reload systemctl restart name.service
2, 在/etc/systemd/system/目錄下建立單獨的unit文件,而不是使用unit源文件的符號連接,具體步驟:
建立unit文件
cp /usr/lib/systemd/system/name.service /etc/systemd/system/name.service
修改
vi /etc/systemd/system/name.service
從新加載
systemctl daemon-reload systemctl restart name.service
3, 觀察unit文件的重寫或修改的差別信息
systemd-delta [EXTENDED] /usr/lib/systemd/system/redis.service → /etc/systemd/system/redis.service.d/limit.conf [REDIRECTED] /etc/systemd/system/default.target → /usr/lib/systemd/system/default.target [EXTENDED] /run/systemd/system/user-0.slice → /run/systemd/system/user-0.slice.d/50-Description.conf ...
關於差別的類型
[MASKED] | 被mask的 [EQUIVALENT] | 內容上沒有區別的文件 [REDIRECTED] | 重定向的文件(指向了其它文件) [OVERRIDEN] | 重寫覆蓋的文件 [EXTENDED] | 使用/etc/systemd/system/unit.d/config_name.conf增長功能的文件 [UNCHANGED] | 沒有修改的文件
1,啓動一個redis的master-slave
複製.service文件
cd /etc/systemd/system/multi-user.target.wants cp redis.service redis-m.service cp redis.service redis-s1.service cp redis.service redis-s2.service
master
[Unit] Description=Redis persistent key-value database After=network.target [Service] ExecStart=/usr/bin/redis-server /opt/rs/7001/redis.conf --daemonize no ExecStop=/usr/libexec/redis-shutdown User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target
slave
[Unit] Description=Redis persistent key-value database After=network.target redis-m.service [Service] ExecStart=/usr/bin/redis-server /opt/rs/7002/redis.conf --daemonize no ExecStop=/usr/libexec/redis-shutdown User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target
開啓開機自啓
#由於是放在默認的target目錄下因此會自動啓動,若是不是 systemctl enable redis-xx
2, 啓動sentinel集羣
複製.service文件
cd /etc/systemd/system cp /usr/lib/systemd/system/redis-sentinel.service sentinel-1.service
配置文件
cat sentinel-1.service [Unit] Description=Redis Sentinel After=network.target redis-m.service [Service] ExecStart=/usr/bin/redis-sentinel /opt/sentinel/8001/redis-sentinel.conf --daemonize no ExecStop=/usr/libexec/redis-shutdown redis-sentinel User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target
cat sentinel-2.service [Unit] Description=Redis Sentinel After=network.target sentinel-1.service [Service] ExecStart=/usr/bin/redis-sentinel /opt/sentinel/8002/redis-sentinel.conf --daemonize no ExecStop=/usr/libexec/redis-shutdown redis-sentinel User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target
cat sentinel-3.service [Unit] Description=Redis Sentinel After=network.target sentinel-2.service [Service] ExecStart=/usr/bin/redis-sentinel /opt/sentinel/8003/redis-sentinel.conf --daemonize no ExecStop=/usr/libexec/redis-shutdown redis-sentinel User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target
設置開機自啓動
systemctl enable sentinel-1.service
須要注意的是由於systemd負責管理和建立服務進程.因此redis須要加上啓動--daemonize no.