吾題帖驚崗
October 24, 2021

Systemd Unit 設定檔參數

於 October 24, 2021  •  1 分鐘  • 68 字

在 linux中,常駐程式的運行,通常是透過 systemd 來管理。

那麼 systemd 的參數有那些呢?請看以下是說明:

服務名稱

[Unit]
Description=Your Server

服務相關文件

Documentation=https://example.com  
Documentation=man:nginx(8)

設定服務啟動的先後相依,例如在網路啟動之後:

After=network.target

行程類型

[Service]
Type=simple

啟動服務指令

ExecStart=/opt/your_command

服務行程 PID(通常配合 forking 的服務使用)

PIDFile=/run/your_server.pid

啟動服務前,執行的指令

ExecStartPre=/opt/your_command

啟動服務後,執行的指令

ExecStartPost=/opt/your_command

停止服務指令

ExecStop=/opt/your_command

停止服務後,執行的指令

ExecStopPost=/opt/your_command

重新載入服務指令

ExecReload=/opt/your_command

服務終止時自動重新啟動

Restart=always

重新啟動時間格時間(預設為 100ms)

RestartSec=3s

啟動服務逾時秒數

TimeoutStartSec=3s

停止服務逾時秒數

TimeoutStopSec=3s

執行時的工作目錄

WorkingDirectory=/opt/your_folder

執行服務的使用者(名稱或 ID 皆可)

User=myuser

執行服務的群組(名稱或 ID 皆可)

User=mygroup

環境變數設定

Environment="VAR=word"

服務輸出訊息導向設定

StandardOutput=syslog

服務錯誤訊息導向設定

StandardError=syslog

設定服務在 Syslog 中的名稱

SyslogIdentifier=your-server

最後設定

[Install]
WantedBy=multi-user.target
comments powered by Disqus
關注

歡迎透過社交平台關注。