如何玩轉 systemd:從基礎到進階
一、概述
systemd是一個系統和服務管理器,systemd是Linux系統中一套基本的構建模塊,提供了一系列工具的集合,用于管理后臺服務、狀態查詢、日志歸檔、設備管理、電源管理和定時任務等許多職責。
systemd作為PID為1的進程,是系統中所有其他進程的父進程。
systemctl是一個命令行工具,用于與systemd進行交互。通過systemctl,用戶可以啟動、停止、重啟、查看狀態以及管理系統中的服務單元
systemd作為后臺服務運行,而systemctl則提供了用戶與systemd交互的接口。用戶通過systemctl發送指令,systemd執行相應的操作
systemctl的命令實際上是向systemd發送請求,告訴systemd要做什么。
systemd通過單元文件(Unit files)來描述和管理不同的系統資源和服務。Systemd 支持 12 種 Unit 文件類型。下面就將我們生產環境中常用到的Service單元文件展開講解。
二、service文件
定義軟件服務的文件通常是systemd服務單元文件,具有.service后綴,這些文件通常位于以下目錄:
# 系統或用戶自定義的配置文件
/etc/systemd/system/
# 軟件運行時生成的配置文件
/run/systemd/system
# 系統或第三方軟件安裝時添加的配置文件。
/usr/lib/systemd/system
Systemd 默認從目錄 /etc/systemd/system/ 讀取配置文件。但是,里面存放的大部分文件都是符號鏈接,指向目錄 /usr/lib/systemd/system/,真正的配置文件存放在那個目錄。
1. service組成部分
service文件主要三大模塊:
[Unit]: #定義與Unit類型無關的通用選項,用于提供unit的描述信息、unit行為及依賴關系等
[Service]: #定義如何啟動、停止、重啟當前服務。
[Install]: #定義如何安裝這個配置文件,即怎樣做到開機啟動。
2. Unit部分
Description: # 對服務的簡單描述
After: # 在哪些服務之后啟動
Before: # 在哪些服務器啟動之前啟動
Requires: # 可以指定服務依賴于哪些服務(強依賴)
Wants: # 可以指定服務依賴于哪些服務(弱依賴)
3. Service部分
EnvironmentFile: # 指定當前服務啟動的環境變量
ExecStart: # 指定服務啟動時執行的命令或腳本
ExecStop: # 指明停止服務要運行的命令或腳本
RestartSec: # 指定服務在重啟時等待的時間,單位為秒
ExecReload: # 指明重啟服務要運行的命令或腳本
Restart: # 重啟設置
KillMode: # 指定停止的方式
Restart: # 指定重啟時的類型
Type: # 指定啟動類型,
type的可選值:
simple # 指定ExecStart字段的進程為主進程
forking # 指定以fork() 子進程執行ExecStart字段的進程
oneshot # 執行一次
notify # 啟動后發送會發送通知信號通知systemd
idle # 等其他任務結束后才運行
Restart的可選值:
no: # 退出后不會重啟
on-success: # 當進程正常退出時(退出碼為0) 執行重啟
on-failure: # 當進程不正常退出時(退出碼不為0) 執行重啟
on-abnormal: # 當被信號終止和超時執行重啟on-abort: 當收到沒有捕捉到的信號終止時執行重啟
on-watchdog: # 當看門狗超時時執行重啟
always: # 一直重啟
KillMode可選值:
control-group: # 殺掉當前進程中所有的進程
process: # 殺掉當前進程的主進程
mixed: # 主進程將收到 SIGTERM 信號,子進程收到 SIGKILL 信號
none: # 不殺掉任何進程
4. Install部分
Alias: # 別名,可使用systemctl command Alias.service
RequiredBy: # 被哪些units所依賴,強依賴
WantedBy: # 被哪些units所依賴,弱依賴
Also: # 安裝本服務的時候還要安裝別的相關服務
Install: # 一般填為WantedBy=multi-user.target
三、service文件樣例
將自己部署的nginx注冊為系統服務:
[Unit]Description=The nginx web and reverse proxy server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
#啟動檢測命令
ExecStartPre=/data/nginx/sbin/nginx -t
#啟動命令
ExecStart=/data/nginx/sbin/nginx -c /data/nginx/conf/nginx.conf
#重載配置文件命令
ExecReload=/data/nginx/sbin/nginx -s reload
#停止命令
ExecStop=/data/nginx/sbin/nginx -s quit
[Install]
WantedBy=multi-user.target
將部署的mysql注冊為系統服務:
[Unit]
Description=Mysql
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
#指定PID文件
PIDFile=/data/mysql/data/centos-linux.shared.pid
#啟動MySQL
ExecStart=/data/mysql/support-files/mysql.server start
#重載
ExecReload=/bin/kill -s HUP $MAINPID
#停止服務
ExecStop=/data/mysql/support-files/mysql.server
stopPrivateTmp=false
[Install]
WantedBy=multi-user.target
四、systemctl命令合集
#啟動服務
systemctl start name.service
#停止服務
systemctl stop name.service
#重啟服務
systemctl restart name.service
#查看服務狀態
systemctl status 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 --all
#設定某服務開機自啟,相當于chkconfig name on
systemctl enable name.service
#設定某服務開機禁止啟動:相當于chkconfig name off
systemctl disable name.service
#查看所有服務的開機自啟狀態,相當于chkconfig --list
systemctl list-unit-files --type service
#用來列出該服務在哪些運行級別下啟用和禁用:chkconfig –list namels /etc/systemd/system/*.wants/name.service
#查看服務是否開機自啟
systemctl is-enabled name.service
#列出失敗的服務
systemctl --failed --type=service
#查看服務的依賴關系
systemctl list-dependencies name.service
#殺掉進程
systemctl kill unitname
#重新加載配置文件
systemctl daemon-reload
#關機
systemctl halt
#重啟服務器
systemctl reboot