簡述systemd的新特性及unit常見類型分析
systemd由來
- Linux一直以來采用init進程但是init有兩個缺點:
1、啟動時間長。Init進程是串行啟動,只有前一個進程啟動完,才會啟動下一個進程。(這也是CentOS5的主要特征)
2、啟動腳本復雜。Init進程只是執行啟動腳本,不管其他事情。腳本需要自己處理各種情況,這使得腳本變得很長而且復雜。 - Init:
CentOS5: Sys init 是啟動速度最慢的,串行啟動過程,無論進程相互之間有無依賴關系。
CentOS6: Upstart init 相對啟動速度快一點有所改進。有依賴的進程之間依次啟動而其他與之沒有依賴關系的則并行同步啟動。
CentOS7: Systemd 與以上都不同。所有進程無論有無依賴關系則都是并行啟動(當然很多時候進程沒有真正啟動而是只有一個信號或者說是標記而已,在真正利用的時候才會真正啟動)。
systemd新特性
- 系統引導時實現服務并行啟動;
- 按需激活進程;
- 系統狀態快照;
- 基于依賴關系定義服務控制邏輯;
unit
Systemd可以管理系統中所有資源。不同的資源統稱為unit(單位)。Unit表示不同類型的systemd對象,通過配置文件進程標識和配置;文件中主要包含了系統服務、監聽socket、保存的系統快照以及其它與init相關的信息。
- unit配置文件
/usr/lib/systemd/system /run/systemd/system /etc/systemd/system
- unit類型
Unit一共分為12種。Sysstemctl –t help 查看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 unit: .automount,文件系統自動點設備;
- Path unit: .path, 用于定義文件系統中的一文件或目錄;
systemd關鍵特性
- 基于socket的激活機制:socket與程序分離;
- 基于bus的激活機制;
- 基于device的激活機制;
- 基于Path的激活機制;
- 系統快照:保存各unit的當前狀態信息于持久存儲設備中;
- 向后兼容sysv init腳本:/etc/init.d/
systemd的不兼容性
- systemctl的命令是固定不變的;
- 非由systemd啟動的服務,systemctl無法與之通信;
systemctl命令
systemctl命令是系統服務管理器指令,它實際上將 service 和 chkconfig 這兩個命令組合到一起。
語法
systemctl [OPTIONS...] COMMAND [NAME...]
舊指令和新指令對比
systemctl [ start | stop | restart | stauts ] 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 condrestart ==> systemctl try-restart NAME.service
重載或重啟服務(先加載,再啟動): systemctl reload-or-restart NAME.servcie
重載或條件式重啟服務:systemctl reload-or-try-restart NAME.service
systemctl [ enable | disable ] NAME.service
設置服務開機自啟: chkconfig NAME on ==> systemctl enable NAME.service
禁止服務開機自啟: chkconfig NAME off ==> systemctl disable NAME.service
查看某服務是否能開機自啟: chkconfig --list NAME ==> systemctl is-enabled NAME.service
禁止某服務設定為開機自啟: systemctl mask NAME.service
取消此禁止: systemctl unmask NAME.servcie
查看系統上所有的服務:systemctl [command] [-type=TYPE] [-all]
查看所有的系統服務: systemctl
查看所有啟動的unit: systemctl list-units
查看所有啟動文件: systemctl list-unit-files
查看所有已啟動的服務:systemctl list-units --type service
查看所有service類型的unit(已啟動及未啟動): chkconfig --lsit ==> systemctl list-units -t service --all
查看某服務當前啟動與否的狀態: systemctl is-active NAME.service
查看服務的依賴關系:systemctl list-dependencies NAME.service
設置運行級別
語法
systemctl [command] [unit.target]
命令
get-default :取得當前的target
set-default :設置指定的target為默認的運行級別
isolate :切換到指定的運行級別
unit.target :為5.1表中列出的運行級別
運行級別:
0 init 0 ==> runlevel0.target, poweroff.target
1 init 1 ==> runlevel1.target, rescue.target
2 init 2 ==> runlevel2.tartet, multi-user.target
3 init 3 ==> runlevel3.tartet, multi-user.target
4 init 4 ==> runlevel4.tartet, multi-user.target
5 init 5 ==> runlevel5.target, graphical.target
6 init 6 ==> runlevel6.target, reboot.target
實例:
systemctl get-default 獲得當前的運行級別
systemctl set-default NAME.target 修改默認運行級別
systemctl set-default multi-user.target 設置默認的運行級別為mulit-user
systemctl isolate multi-user.target 在不重啟的情況下,切換到運行級別mulit-user下
systemctl isolate graphical.target 在不重啟的情況下,切換到圖形界面下
systemctl rescue 切換至救援模式
systemctl emergency 強制進入緊急救援模式
其他命令
systemctl halt 關機
systemctl poweroff 關機
systemctl reboot 重啟
systemctl suspend 掛起
systemctl hibernate 快照
systemctl hybrid-sleep 快照并掛起
Unit 的配置文件
1.配置目錄
/usr/lib/systemd/system/
2.文件組成
[Unit]:定義與Unit類型無關的通用選項;用于提供unit的描述信息、unit行為及依賴關系等;
[Service]:與特定類型相關的專用選項;此處為Service類型;
[Install]:定義由“systemctl enable”以及"systemctl disable“命令在實現服務啟用或禁用時用到的一些選項;
3.Unit段的常用選項
Description:描述信息; 意義性描述;
After:定義unit的啟動次序;表示當前unit應該晚于哪些unit啟動;其功能與Before相反;
Requies:依賴到的其它units;強依賴,被依賴的units無法激活時,當前unit即無法激活;
Wants:依賴到的其它units;弱依賴;
Conflicts:定義units間的沖突關系;
4.Service段的常用選項
Type:用于定義影響ExecStart及相關參數的功能的unit進程啟動類型;
類型:
simple:默認值,執行ExecStart指定的命令,啟動主進程
forking:以 fork 方式從父進程創建子進程,創建后父進程會立即退出
oneshot:一次性進程,Systemd 會等當前服務退出,再繼續往下執行
dbus:當前服務通過D-Bus啟動
notify:當前服務啟動完畢,會通知Systemd,再繼續往下執行
idle:若有其他任務執行完畢,當前服務才會運行
EnvironmentFile:環境配置文件;
ExecStart:指明啟動unit要運行命令或腳本; ExecStartPre, ExecStartPost
ExecStop:指明停止unit要運行的命令或腳本;
Restart:定義何種情況 Systemd 會自動重啟當前服務,可能的值包括always(總是重啟)、on-success、on-failure、on-abnormal、on-abort、on-watchdog
5.Install段的常用選項:
Alias:
RequiredBy:被哪些units所依賴;
WantedBy:被哪些units所依賴;
6.新創建、修改unit文件需重載此配置文件
systemctl daemon-reload
實例:編譯安裝的nginx并通過systemd來管理
- 安裝工具
[root@localhost ~]#yum -y install gcc gcc-c++ autoconf automake [root@localhost ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
- 下載nginx
[root@localhost ~]# cd /usr/local/ [root@localhost local]# wget http://nginx.org/download/nginx-1.13.0.tar.gz
- 解壓編譯
[root@localhost local]# tar -zxvf nginx-1.13.0.tar.gz [root@localhost local]# cd nginx-1.13.0 [root@localhost nginx-1.13.0]# ./configure
- 安裝
[root@localhost nginx-1.13.0]# make && make install
- 創建nginx.service文件
[root@localhost ~]#vim /usr/lib/systemd/system/nginx.service [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
- 檢查nginx是否啟動
[root@localhost ~]# ps aux | grep nginx root 2278 0.0 0.0 112664 968 pts/0 R+ 05:21 0:00 grep --color=auto nginx
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/101092