Debian10 添加系統(tǒng)服務(wù)(systemctl管理)
Debian10 添加系統(tǒng)服務(wù)(systemctl管理)檢查是否安裝了systemctl 可以使用以下命令進(jìn)行檢查1# systemd --version如果沒(méi)有安裝可以網(wǎng)上搜索看系統(tǒng)是否支持安裝 創(chuàng)建服務(wù)文件 服務(wù)文件的目錄在1cd /lib/systemd/system新建服
Debian10 添加系統(tǒng)服務(wù)(systemctl管理)檢查是否安裝了systemctl 可以使用以下命令進(jìn)行檢查1# systemd --version如果沒(méi)有安裝可以網(wǎng)上搜索看系統(tǒng)是否支持安裝 創(chuàng)建服務(wù)文件 服務(wù)文件的目錄在1cd /lib/systemd/system新建服
可以使用以下命令進(jìn)行檢查
1 | # systemd --version |
如果沒(méi)有安裝可以網(wǎng)上搜索看系統(tǒng)是否支持安裝
服務(wù)文件的目錄在
1 | cd /lib/systemd/system |
新建服務(wù)文件,簡(jiǎn)單模板如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [Unit] # 簡(jiǎn)短描述 Description=node_exporter # 文檔描述 Documentation=node_exporter Monitoring System # 如果該字段指定的 Unit 也要啟動(dòng),那么必須在當(dāng)前 Unit 之前啟動(dòng) After=network.target [Service] User=服務(wù)啟動(dòng)的用戶名 Group=用戶所在組名 ExecStart=可執(zhí)行程序的路徑 [Install] WantedBy=multi-user.target |
保存文件為test.service
1 | systemctl start test |
Unit 塊
1 2 3 4 5 6 7 8 9 10 | Description:簡(jiǎn)短描述 Documentation:文檔地址 Requires:當(dāng)前 Unit 依賴的其他 Unit,如果它們沒(méi)有運(yùn)行,當(dāng)前 Unit 會(huì)啟動(dòng)失敗 Wants:與當(dāng)前 Unit 配合的其他 Unit,如果它們沒(méi)有運(yùn)行,當(dāng)前 Unit 不會(huì)啟動(dòng)失敗 BindsTo:與Requires類似,它指定的 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)失敗 |
Install 塊
1 2 3 4 | WantedBy:它的值是一個(gè)或多個(gè) Target,當(dāng)前 Unit 激活時(shí)(enable)符號(hào)鏈接會(huì)放入/etc/systemd/system目錄下面以 Target 名 + .wants后綴構(gòu)成的子目錄中 RequiredBy:它的值是一個(gè)或多個(gè) Target,當(dāng)前 Unit 激活時(shí),符號(hào)鏈接會(huì)放入/etc/systemd/system目錄下面以 Target 名 + .required后綴構(gòu)成的子目錄中 Alias:當(dāng)前 Unit 可用于啟動(dòng)的別名 Also:當(dāng)前 Unit 激活(enable)時(shí),會(huì)被同時(shí)激活的其他 Unit |
Service 塊
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Type:定義啟動(dòng)時(shí)的進(jìn)程行為。它有以下幾種值。 Type=simple:默認(rèn)值,執(zhí)行ExecStart指定的命令,啟動(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ù)通過(guò)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)當(dāng)前服務(wù)的命令 ExecStartPre:?jiǎn)?dòng)當(dāng)前服務(wù)之前執(zhí)行的命令 ExecStartPost:?jiǎn)?dòng)當(dāng)前服務(wù)之后執(zhí)行的命令 ExecReload:重啟當(dāng)前服務(wù)時(shí)執(zhí)行的命令 ExecStop:停止當(dāng)前服務(wù)時(shí)執(zhí)行的命令 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)境變量 |
附官方說(shuō)明:https://www.freedesktop.org/software/systemd/man/systemd.unit.html
1 2 3 4 5 6 7 8 9 | systemctl start node_exporter??開(kāi)啟服務(wù) systemctl stop node_exporter?? 關(guān)閉服務(wù) systemctl restart node_exporter????重啟服務(wù) systemctl status node_exporter????查看服務(wù)狀態(tài) systemctl enable node_exporter????將服務(wù)設(shè)置為開(kāi)機(jī)自啟動(dòng) systemctl disable node_exporter????禁止服務(wù)開(kāi)機(jī)自啟動(dòng) systemctl is-enabled node_exporter????查看服務(wù)是否開(kāi)機(jī)啟動(dòng) systemctl list-unit-files|grep enabled????查看開(kāi)機(jī)啟動(dòng)的服務(wù)列表 systemctl --failed????查看啟動(dòng)失敗的服務(wù)列表 |
版權(quán)所有:深圳市網(wǎng)商在線科技有限公司
友情鏈接: