note_16.3_systemd

Systemd的新特性:

  • 系統(tǒng)引導(dǎo)時(shí)實(shí)現(xiàn)服務(wù)并行啟動(dòng);
  • 按需激活進(jìn)程;
  • 系統(tǒng)狀態(tài)快照;
  • 基于依賴關(guān)系定義服務(wù)控制邏輯;

核心概念:unit

unit由其相關(guān)配置文件進(jìn)行標(biāo)識(shí)、識(shí)別和配置;文件中主要包含了系統(tǒng)服務(wù)、監(jiān)聽的socket、保存的快照以及其它與init相關(guān)的信息; 這些配置文件主要保存在:
/usr/lib/systemd/system
/run/systemd/system
/etc/systemd/system

unit的常見類型:

├── Automunt unit: .automount,文件系統(tǒng)自動(dòng)點(diǎn)設(shè)備;
├── Device unit: .device,用于定義內(nèi)核識(shí)別的設(shè)備;
├── Mount unit: .mount,定義文件系統(tǒng)掛載點(diǎn);
├── Path unit: .path, 用于定義文件系統(tǒng)中的一文件或目錄;
├── Service unit:文件擴(kuò)展名為.service,用于定義系統(tǒng)服務(wù);
├── Snapshot unit: .snapshot, 管理系統(tǒng)快照;
├── Socket unit: .socket,用于標(biāo)識(shí)進(jìn)程間通信用到的socket文件;
├── Swap unit: .swap, 用于標(biāo)識(shí)swap設(shè)備;
└── Target unit:文件擴(kuò)展為.target,用于模擬實(shí)現(xiàn)“運(yùn)行級(jí)別”;

關(guān)鍵特性:

基于socket的激活機(jī)制:socket與程序分離;
基于bus的激活機(jī)制;
基于device的激活機(jī)制;
基于Path的激活機(jī)制;
系統(tǒng)快照:保存各unit的當(dāng)前狀態(tài)信息于持久存儲(chǔ)設(shè)備中;
向后兼容sysv init腳本 /etc/init.d/

不兼容:
systemctl的命令是固定不變的;
非由systemd啟動(dòng)的服務(wù),systemctl無法與之通信;

管理系統(tǒng)服務(wù):

CentOS 7: service類型的unit文件;

syscemctl命令:

Control the systemd system and service manager

systemctl [OPTIONS...] COMMAND [NAME...]

啟動(dòng): service NAME start ==> systemctl start NAME.service
停止: service NAME stop ==> systemctl stop NAME.service
重啟: service NAME restart ==> systemctl restart NAME.service
狀態(tài): service NAME status ==> systemctl status NAME.service
條件式重啟:service NAME condrestart ==> systemctl try-restart NAME.service
重載或重啟服務(wù): systemctl reload-or-restart NAME.servcie
重載或條件式重啟服務(wù):systemctl reload-or-try-restart NAME.service

查看某服務(wù)當(dāng)前激活與否的狀態(tài): systemctl is-active NAME.service
查看所有已激活的服務(wù):systemctl list-units --type service
查看所有服務(wù)(已激活及未激活): chkconfig --lsit ==> systemctl list-units -t service --all

設(shè)置服務(wù)開機(jī)自啟: chkconfig NAME on ==> systemctl enable NAME.service
禁止服務(wù)開機(jī)自啟: chkconfig NAME off ==> systemctl disable NAME.service
查看某服務(wù)是否能開機(jī)自啟: chkconfig --list NAME ==> systemctl is-enabled NAME.service

禁止某服務(wù)設(shè)定為開機(jī)自啟: systemctl mask NAME.service
取消此禁止: systemctl unmask NAME.servcie

查看服務(wù)的依賴關(guān)系:systemctl list-dependencies NAME.service

管理target units:

運(yùn)行級(jí)別:
├── 0 ==> runlevel0.target, poweroff.target
├── 1 ==> runlevel1.target, rescue.target
├── 2 ==> runlevel2.tartet, multi-user.target
├── 3 ==> runlevel3.tartet, multi-user.target
├── 4 ==> runlevel4.tartet, multi-user.target
├── 5 ==> runlevel5.target, graphical.target
└── 6 ==> runlevel6.target, reboot.target

級(jí)別切換: init N ==> systemctl isolate NAME.target

查看級(jí)別: runlevel ==> systemctl list-units --type target
查看所有級(jí)別: systemctl list-units -t target -a

獲取默認(rèn)運(yùn)行級(jí)別:systemctl get-default
修改默認(rèn)運(yùn)行級(jí)別: systemctl set-default NAME.target

切換至緊急救援模式: systemctl rescue
切換至emergency模式: systemctl emergency

其它常用命令:
├── 關(guān)機(jī): systemctl halt, systemctl poweroff
├── 重啟: systemctl reboot
├── 掛起: systemctl suspend
├── 快照: systemctl hibernate
└── 快照并掛起: systemctl hybrid-sleep

service unit file:

  • 文件通常由三部分組成:

    • [Unit]:定義與Unit類型無關(guān)的通用選項(xiàng);用于提供unit的描述信息、unit行為及依賴關(guān)系等;
    • [Service]:與特定類型相關(guān)的專用選項(xiàng);此處為Service類型;
    • [Install]:定義由“systemctl enable”以及"systemctl disable“命令在實(shí)現(xiàn)服務(wù)啟用或禁用時(shí)用到的一些選項(xiàng);
  • Unit段的常用選項(xiàng):
    ├── Description:描述信息; 意義性描述;
    ├── After:定義unit的啟動(dòng)次序;表示當(dāng)前unit應(yīng)該晚于哪些unit啟動(dòng);其功能與Before相反;
    ├── Requies:依賴到的其它units;強(qiáng)依賴,被依賴的units無法激活時(shí),當(dāng)前unit即無法激活;
    ├── Wants:依賴到的其它units;弱依賴;
    └── Conflicts:定義units間的沖突關(guān)系;

  • Service段的常用選項(xiàng):
    ├── Type:用于定義影響ExecStart及相關(guān)參數(shù)的功能的unit進(jìn)程啟動(dòng)類型;
    ????simple:
    ????forking:
    ????oneshot:
    ????dbus:
    ????notify:
    ????idle:
    ├── EnvironmentFile:環(huán)境配置文件;
    ├── ExecStart:指明啟動(dòng)unit要運(yùn)行命令或腳本; ExecStartPre, ExecStartPost
    ├── ExecStop:指明停止unit要運(yùn)行的命令或腳本;
    └── Restart:

  • Install段的常用選項(xiàng):
    ├── Alias:
    ├── RequiredBy:被哪些units所依賴;
    └── WantedBy:被哪些units所依賴;

注意:對(duì)于新創(chuàng)建的unit文件或,修改了的unit文件,要通知systemd重載此配置文件;
# systemctl daemon-reload

練習(xí):為當(dāng)前系統(tǒng)的httpd服務(wù)提供一個(gè)unit文件;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容