SystemdSystemd(system daemon)實(shí)現(xiàn)系統(tǒng)服務(wù)間的依賴關(guān)系,并依此實(shí)現(xiàn)系統(tǒng)初始化時(shí)服務(wù)的并行啟動(dòng),同時(shí)達(dá)到降低Shell的系統(tǒng)開銷,最終替代init。 Systemd采用了一下新技術(shù):
Systemd Unit學(xué)習(xí)Systemd的第一步,就是搞懂 Unit(單元)是什么? Unit就是Systemd的最小 功能/管理 單位,是單個(gè)進(jìn)程的描述。一個(gè)個(gè)小的Unit相互調(diào)用和依賴,組成一個(gè)龐大的任務(wù)管理系統(tǒng),這就是Systemd的基本思想。 由于Systemd要做到事情太多,導(dǎo)致Unit有很多種不同的類型:
所有的Unit文件都存放在 /usr/lib/systemd/system和/etc/systemd/system Service Unit
[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:默認(rèn)值,執(zhí)行ExecStart指定的命令,啟動(dòng)主進(jìn)程 forking:以 fork 方式從父進(jìn)程創(chuàng)建子進(jìn)程,創(chuàng)建后父進(jìn)程會(huì)立即退出 oneshot:一次性進(jìn)程,Systemd 會(huì)等當(dāng)前服務(wù)退出,再繼續(xù)往下執(zhí)行 dbus:當(dāng)前服務(wù)通過D-Bus啟動(dòng) notify:當(dāng)前服務(wù)啟動(dòng)完畢,會(huì)通知systemd再繼續(xù)往下執(zhí)行 idle:若有其他任務(wù)執(zhí)行完畢,當(dāng)前服務(wù)才會(huì)運(yùn)行 EnvironmentFile:環(huán)境配置文件; ExecStart:指明啟動(dòng)unit要運(yùn)行命令或腳本; ExecStartPre:在ExecStart之前運(yùn)行; ExecStartPost:在ExecStart之后運(yùn)行; ExecReload: 指明重新加載配置的命令或腳本; #示例 ExecReload=/bin/kill -HUP $MAINPID ExecStop:指明停止unit要運(yùn)行的命令或腳本; Restart:當(dāng)設(shè)定Restart=1時(shí),則當(dāng)次daemon服務(wù)意外終止后,會(huì)再次自動(dòng)啟動(dòng)。 [Install] 段的常用選項(xiàng): Alias:別名,可使用systemctl command Alias.service; RequiredBy:被哪些units所依賴; WantedBy:被哪些units所依賴 注意:對于新建或修改unit文件,需要通過命令 systemctl daemon-reload 重新加載。 Timer Unit
**定義Timer調(diào)用的my-timer.service **[Unit] Description = MyTimer [Service] ExecStart = /bin/echo '...Hello MyTimer...'**定義調(diào)用Service的my-timer.timer**[Unit] Description = Runs mytimer every minutes [Timer] OnUnitActiveSec = 1m Unit = mytimer.service [Install]WantedBy = multi-user.target # [Timer] 字段 OnActiveSec:定時(shí)器生效后,多少時(shí)間開始執(zhí)行任務(wù) Socket Unit
Target Unit
Systemd Command# 查看單元狀態(tài) ## active (running): 進(jìn)程持續(xù)的運(yùn)行中 ## active (exited): 進(jìn)程成功完成一次執(zhí)行 ## active (waiting): 進(jìn)程等待中 ## inactive: 未運(yùn)行 $ systemctl status [UnitName] # 查看是否已啟用該單元 ## enable、disable或static ## static是指對應(yīng)的Unit文件中沒有定義[Install]區(qū)域,因此無法配置為開機(jī)啟動(dòng)服務(wù)。 $ systemctl is-enabled [UnitName] # 開機(jī)自動(dòng)執(zhí)行該單元 $ systemctl enable [UnitName] # 關(guān)閉開機(jī)自動(dòng)執(zhí)行 $ systemctl disable [UnitName] # 啟動(dòng)單元 $ systemctl start [UnitName] # 關(guān)閉單元 $ systemctl stop [UnitName] # 重啟單元 $ systemctl restart [UnitName] # 重新加載服務(wù)配置,而不中斷服務(wù) $ systemctl reload [UnitName] # 類似于 nginx -s reload # 殺死單元進(jìn)程 $ systemctl kill [UnitName] # 禁止服務(wù),無法啟動(dòng)或開機(jī)啟動(dòng) $ systemctl mask [UnitName] # List units that systemd currently has in memory. $ systemctl list-units # List unit files installed on the system, in combination with their enablement state (as reported by is-enabled). $ systemctl list-unit-files # Shows units required and wanted by the specified unit. $ systemctl list-dependencies Systemd 架構(gòu)圖Systemd官網(wǎng)翻譯: |
|