Linux 系統(tǒng)資源管理 | 資源配置文件
新 Linux 系統(tǒng)中,使用 systemd 管理所有系統(tǒng)資源
所有資源成為 unit,每一個(gè) unit 都有一個(gè)配置文件
有時(shí)候我們需要?jiǎng)?chuàng)建或修改 unit 的配置文件
配置文件可以用文本編輯器打開,比如 vim。
也可以用命令 systemctl cat nginx.service
# systemctl cat nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]
用來定義 Unit 的元數(shù)據(jù),以及配置與其他 Unit 的關(guān)系
有以下參數(shù):
-
Description:資源描述
-
Documentation:文檔地址
-
Requires:當(dāng)前 Unit 依賴的其他 Unit,如果它們沒有運(yùn)行,當(dāng)前 Unit 會(huì)啟動(dòng)失敗
-
Wants:與當(dāng)前 Unit 配合的其他 Unit,如果它們沒有運(yùn)行,當(dāng)前 Unit 不會(huì)啟動(dòng)失敗
-
BindsTo:它指定的 Unit 如果退出,會(huì)導(dǎo)致當(dāng)前 Unit 停止運(yùn)行
-
Before:如果該字段指定的 Unit 也要啟動(dòng),那么必須在當(dāng)前 Unit 之后啟動(dòng)
-
After:如果該字段指定的 Unit 也要啟動(dòng),那么必須在當(dāng)前 Unit 之前啟動(dòng)
-
Conflicts:這里指定的 Unit 不能與當(dāng)前 Unit 同時(shí)運(yùn)行
-
Condition...:當(dāng)前 Unit 運(yùn)行必須滿足的條件,否則不會(huì)運(yùn)行
-
Assert...:當(dāng)前 Unit 運(yùn)行必須滿足的條件,否則會(huì)報(bào)啟動(dòng)失敗
[Service]
用來 service 的配置,只有服務(wù)資源有這個(gè)模塊
-
Type:定義啟動(dòng)時(shí)的進(jìn)程行為
-
Type=simple:默認(rèn)值,啟動(dòng)主進(jìn)程
-
Type=forking:以 fork 方式從父進(jìn)程創(chuàng)建子進(jìn)程,創(chuàng)建后父進(jìn)程會(huì)立即退出
-
Type=oneshot:一次性進(jìn)程,Systemd 會(huì)等當(dāng)前服務(wù)退出,再繼續(xù)往下執(zhí)行
-
Type=dbus:當(dāng)前服務(wù)通過 D-Bus 啟動(dòng)
-
Type=notify:當(dāng)前服務(wù)啟動(dòng)完畢,會(huì)通知 Systemd,再繼續(xù)往下執(zhí)行
-
Type=idle:若有其他任務(wù)執(zhí)行完畢,當(dāng)前服務(wù)才會(huì)運(yùn)行
-
-
ExecStart:?jiǎn)?dòng)命令
-
ExecStartPre:?jiǎn)?dòng)服務(wù)之前執(zhí)行的命令
-
ExecStartPost:?jiǎn)?dòng)服務(wù)之后執(zhí)行的命令
-
ExecReload:重新加載服務(wù)的命令
-
ExecStop:停止服務(wù)的命令
-
ExecStopPost:停止當(dāng)其服務(wù)之后執(zhí)行的命令
-
RestartSec:自動(dòng)重啟當(dāng)前服務(wù)間隔的秒數(shù)
-
Restart:定義何種情況 Systemd 會(huì)自動(dòng)重啟當(dāng)前服務(wù),可能的值包括:
-
always
-
on-success
-
on-failure
-
on-abnormal
-
on-abort
-
on-watchdog
-
-
TimeoutSec:定義 Systemd 停止當(dāng)前服務(wù)之前等待的秒數(shù)
-
Environment:指定環(huán)境變量
[Install]
用來定義啟動(dòng)方式
-
WantedBy:一個(gè)或多個(gè) Target,Unit 軟鏈接會(huì)放入 /etc/systemd/system 目錄下面以 Target 名 + .wants 后綴構(gòu)成的子目錄中
-
RequiredBy:一個(gè)或多個(gè) Target,Unit 軟鏈接會(huì)放入 /etc/systemd/system 目錄下面以 Target 名 + .required 后綴構(gòu)成的子目錄中
-
Alias:Unit 可用于啟動(dòng)的別名
-
Also:Unit 激活(enable)時(shí),會(huì)被同時(shí)激活的其他 Unit
配置文件中設(shè)置的 target,相當(dāng)于一個(gè) unit 組
啟動(dòng)某個(gè) target,相當(dāng)于啟動(dòng)了組內(nèi)所有的 unit
target 也可以用 systemctl 查看
-
查看所有 target
systemctl list-unit-files --type=target -
查看 target 包含的 unit
systemctl list-dependencies multi-user.target -
查看系統(tǒng)啟動(dòng)時(shí)的默認(rèn) target
systemctl get-default -
設(shè)置啟動(dòng)時(shí)的默認(rèn) target
systemctl set-default multi-user.target

浙公網(wǎng)安備 33010602011771號(hào)