终端关闭时 Systemd 关闭

Nin*_*MAN 2 ubuntu 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
Run Code Online (Sandbox Code Playgroud)

在服务器中,当我断开 ssh 连接时,服务就会终止并导致502 Bad Gateway。我必须 ssh 并运行systemctl --user start os.service才能启动并使其工作。一旦我关闭终端,os.service 就会再次终止。

Ste*_*itt 7

您\xe2\x80\x99 正在从用户管理器( --user) 运行该服务,并且显然您logind\xe2\x80\x99 没有为您启用延迟。因此,每当您注销时,您的会话(包括其中启动的任何服务)都会关闭。

\n\n

要改变这种行为,您需要启用 lingering

\n\n
sudo loginctl enable-linger $USER\n
Run Code Online (Sandbox Code Playgroud)\n\n

这将在启动时为您启动一个用户管理器,并允许您的服务在您的登录会话中继续存在。

\n\n

对于这样的系统服务,更好的方法实际上是作为自己的用户运行它,由主 systemd 实例管理。

\n