Systemd 服务在其用户出口处停止 - debian 9stretch

Systemd 服务在其用户出口处停止 - debian 9stretch

所以我遇到了在设置我的 teampeak 服务器时遇到的问题(正确!)。 Teamspeak Server 及其配置完全没问题。当我希望它在系统启动时启动时,我的问题就开始了。现在介绍如何创建自动启动脚本等。我已经完成了,并且它在过去已经起作用了。但这次我只想使用 systemd 服务来完成此操作,因为 teampeak 已经提供了一个 startscript。

因此,在给出一些背景信息后,让我们开始解决问题:通过我现在正在使用的服务配置(见下文),teamspeak 服务器在系统启动时完美启动。但是,当我使用用户“teamspeak”登录执行一些操作然后输入 exit 时,teamspeak 服务器将关闭并随后重新启动。

[Unit]
Description=TeamSpeak 3 Server

[Service]
ExecStart=/teamspeak/ts3server_startscript.sh start
ExecStop=/teamspeak/ts3server_startscript.sh stop
PIDFile=/teamspeak/ts3server.pid
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=teamspeak_server
User=teamspeak
Group=teamspeak
Type=Forking
[Install]
WantedBy=multi-user.target

因此,重新启动来自“Restart=always”,我百分百确定,teamspeak-shutdown 来自该服务。我已经尝试过“RemainAfterExit = true”,但唯一所做的事情是,在用户注销后不会重新启动teamspeak服务器(因为它让我们认为服务仍然处于活动状态,但进程全部关闭。至少是这样我如何理解该服务正在运行以及为什么它停止了我的 teampeak 服务器。

简而言之:当teamspeak用户退出其会话时,如何防止服务调用ExecStop命令(在我的例子中是putty,ssh连接)

以下是所有 systemd.service 选项的总结:点击我

尽管它与我上面写的内容相反,但我也编写了一个脚本,将 ts3server_minimal_startscript.sh 包装到屏幕会话中。该脚本在手动使用时工作得非常好,但通过服务使用它时我仍然遇到完全相同的问题。正如我所说,我更喜欢非屏幕版本,因为 ts3server_startscript.sh 已经为 ts-server 创建了自己的进程。

编辑:我忘了说,我在 /etc/systemd/system/teamspeak.service 创建了该服务。根据请求,这是我退出 teampeak 用户之前的 systemctl 状态答案:

● teamspeak.service - TeamSpeak 3 Server
   Loaded: loaded (/etc/systemd/system/teamspeak.service; enabled; vendor 
preset
: enabled)
   Active: active (running) since Sat 2018-04-14 17:35:08 CEST; 23h a
go
  Process: 3344 ExecStop=/teamspeak/ts3server_startscript.sh stop 
(code=exited,
status=0/SUCCESS)
 Main PID: 3361 (ts3server)
    Tasks: 17 (limit: 4915)
   CGroup: /system.slice/teamspeak.service
           └─3361 ./ts3server

Apr 14 17:35:08 srv83030 systemd[1]: Started TeamSpeak 3 Server.
Apr 14 17:35:08 srv83030 teamspeak_server[3357]: Starting the TeamSpeak 3 
server

退出 teampeak 用户后 1 毫秒:

● teamspeak.service - TeamSpeak 3 Server
   Loaded: loaded (/etc/systemd/system/teamspeak.service; enabled; vendor 
preset
: enabled)
   Active: active (running) since Sun 2018-04-15 16:55:11 CEST; 1ms a
go
  Process: 5372 ExecStop=/teamspeak/ts3server_startscript.sh stop 
(code=exited,
status=0/SUCCESS)
 Main PID: 5388 (ts3server_start)
    Tasks: 0 (limit: 4915)
   CGroup: /system.slice/teamspeak.service
           └─5388 /bin/sh /teamspeak/ts3server_startscript.sh start

Apr 15 16:55:11 srv83030 systemd[1]: Started TeamSpeak 3 Server.

退出 Teamspeak 用户 2 秒后:

● teamspeak.service - TeamSpeak 3 Server
   Loaded: loaded (/etc/systemd/system/teamspeak.service; enabled; vendor 
preset
: enabled)
   Active: active (running) since Sun 2018-04-15 16:55:11 CEST; 2s ag
o
  Process: 5372 ExecStop=/teamspeak/ts3server_startscript.sh stop 
(code=exited,
status=0/SUCCESS)
 Main PID: 5392 (ts3server)
    Tasks: 16 (limit: 4915)
   CGroup: /system.slice/teamspeak.service
           └─5392 ./ts3server

Apr 15 16:55:11 srv83030 systemd[1]: Started TeamSpeak 3 Server.
Apr 15 16:55:11 srv83030 teamspeak_server[5388]: Starting the TeamSpeak 3 
server

在我使用 teampeak 用户重新登录后又出现了一个:

● teamspeak.service - TeamSpeak 3 Server
   Loaded: loaded (/etc/systemd/system/teamspeak.service; enabled; vendor 
preset: enabled)
   Active: active (running) since Sun 2018-04-15 16:55:11 CEST; 3min 43s ago
  Process: 5372 ExecStop=/teamspeak/ts3server_startscript.sh stop 
(code=exited, status=0/SUCCESS)
 Main PID: 5392 (ts3server)
    Tasks: 16 (limit: 4915)
   CGroup: /system.slice/teamspeak.service
           └─5392 ./ts3server

Apr 15 16:55:11 srv83030 systemd[1]: Started TeamSpeak 3 Server.
Apr 15 16:55:11 srv83030 teamspeak_server[5388]: Starting the TeamSpeak 3 
server

EDIT2:也许知道 teampeak 用户的主目录位于 /teamspeak 也很有用。所以起始脚本也在那里。

答案1

您可以尝试将 systemd 配置为在所需用户注销时不终止用户进程:

sudo loginctl enable-linger teamspeak

答案2

我认为该服务不应该为特定用户/组(teamspeak)运行。

首先,备份所有文件(服务文件等),然后禁用它:

# systemctl disable teamspeak.service

另外,删除/删除您在系统上复制的物理teamspeak.service 文件。

以用户身份 ssh 进入您的服务器root,然后尝试以下操作:

编辑你的teamspeak.service文件:

[Unit]
Description=TeamSpeak 3 Server
After=network.target

[Service]
ExecStart=/teamspeak/ts3server_startscript.sh start
ExecStop=/teamspeak/ts3server_startscript.sh stop
RestartSec=1s


[Install]
WantedBy=default.target

接下来,安装systemd服务单元并启用它,以便它将在启动时执行:

# cp  teamspeak.service /etc/systemd/system/
# chmod 0664 /etc/systemd/system/teamspeak.service
# systemctl daemon-reload
# systemctl enable teamspeak.service

启动服务:

# systemctl start teamspeak.service

寻找 OK 响应(绿点)。

查找服务是否存在或已配置:

# systemctl list-unit-files --type=service | grep teamspeak

询问服务:

# service teamspeak status

注意:systemd 参考 URL -https://wiki.debian.org/systemd

相关内容