簡(jiǎn)單添加 systemd 服務(wù)
目錄
1 示例
?
注意事項(xiàng):
存放目錄:
/etc/systemd/system
systemd 加載服務(wù)的路徑可參考:systemd.unit 中文手冊(cè) - 金步國(guó) 中的 "單元目錄(單元文件加載路徑)"
若
systemctl start
找不到該服務(wù),可執(zhí)行systemctl daemon-reload
查看日志可用
journalctl -u <服務(wù)名>
,systemctl status
也會(huì)顯示部分日志
2 Unit 字段
Description
描述BindsTo
要依賴(lài)哪些服務(wù),任一依賴(lài)的服務(wù)停止后,本服務(wù)也會(huì)被停止After
在哪些服務(wù)啟動(dòng)之后再啟動(dòng)
詳細(xì)字段解析:systemd.unit 中文手冊(cè) - 金步國(guó)
3 Service 字段
User
設(shè)置進(jìn)程在執(zhí)行時(shí)使用的用戶(hù),默認(rèn)為 rootWorkingDirectory
設(shè)置進(jìn)程的工作目錄,可以設(shè)置為 "~"ExecStart
服務(wù)啟動(dòng)時(shí)執(zhí)行的命令Restart
設(shè)置哪種狀態(tài)下需要自動(dòng)重啟服務(wù),"on-failure" 為服務(wù)異常退出后重啟RestartSec
多少時(shí)間后自動(dòng)重啟,默認(rèn)單位為秒
詳細(xì)字段解析:systemd.exec 中文手冊(cè) - 金步國(guó)、systemd.service 中文手冊(cè) - 金步國(guó)
4 Install 字段
WantedBy
設(shè)置服務(wù)列表,執(zhí)行systemctl enable
后,任一服務(wù)啟動(dòng)時(shí),本服務(wù)也會(huì)跟著啟動(dòng)Also
設(shè)置服務(wù)列表,執(zhí)行systemctl enable
或systemctl diable
后,也會(huì)同時(shí)應(yīng)用到這些服務(wù)
詳細(xì)字段解析:systemd.unit 中文手冊(cè) - 金步國(guó)