終端關閉時 Systemd 關閉

終端關閉時 Systemd 關閉

我的 systemd 服務文件的名稱為os.service並具有以下配置行。

#  This systemd service file will help supervise os service
[Unit]
Description=Os Server
After= network.target

[Service]

# Preferably configure a non-privileged user
# User=deploy
# Group=deploy

# Environment variables shared across the config
#EnvironmentFile=  # environment file to be used, includes RACK_ENV
EnvironmentFile=/home/deploy/test/shared/.env
SyslogIdentifier=test
PIDFile=/home/deploy/test/shared/tmp/server.pid

# Specify the path to your test application root
# WorkingDirectory=/home/deploy/test/

# Start/Reload/Stop syntax
ExecStart=/home/deploy/test/current

# TimeoutSec=15
# TimeoutStopSec=10
RestartSec=5s           

# Restart os, always if it dies for some reason
Restart=always

[Install]
WantedBy=multi-user.target

在伺服器中,當我斷開 ssh 連線時,服務就會終止並導致502錯誤的網關。我必須 ssh 並運行systemctl --user start os.service才能啟動並使其工作。一旦我關閉終端,os.service 就會再次終止。

答案1

您正在從使用者管理員 ( --user) 執行該服務,並且顯然您logind沒有為您啟用延遲。因此,每當您登出時,您的會話(包括其中啟動的任何服務)都會關閉。

要改變這種行為,您需要啟用延遲:

sudo loginctl enable-linger $USER

這將在啟動時為您啟動使用者管理器,並允許您的服務在登入工作階段中繼續存在。

對於這樣的系統服務,更好的方法實際上是作為自己的使用者運行它,由主 systemd 實例管理。

相關內容