无法使用 systemctl 在 tmux 中启动 Minecraft 服务器

无法使用 systemctl 在 tmux 中启动 Minecraft 服务器

我正在尝试自动化我的 Minecraft 服务器,因为手动管理 tmux 会话一段时间后就会变得非常乏味。下面是我编写的 systemd 服务文件,可帮助我实现此目的:

[Unit]
Description=Minecraft Server: %i
After=network.target

[Service]
WorkingDirectory=/opt/minecraft/servers/%i
User=minecraft
Group=minecraft

Restart=on-failure

ExecStart=/usr/bin/tmux new -s mc-%i -d '/usr/bin/java -Xms1G -Xmx6G -jar forge-universal.jar nogui' bash

ExecStop=/usr/bin/tmux send -t mc-%i 'say SERVER WILL SHUT DOWN IN 10 SECONDS' ENTER
ExecStop=/bin/sleep 10
ExecStop=/usr/bin/tmux send -t mc-%i 'stop' ENTER

[Install]
WantedBy=multi-user.target

手动执行该ExecStart行非常完美,Java 进程被整齐地放入一个独立的 tmux 会话中,我可以随时附加到该会话中。

尝试通过启动服务器systemctl start minecraft@creative失败了,日志中充满了几十条错误消息,最后失败了:

Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Service hold-off time over, scheduling restart.
Oct 11 21:34:45 kingcolour systemd[1]: Stopped Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: Starting Minecraft Server: private...
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Control process exited, code=exited status=1
Oct 11 21:34:45 kingcolour systemd[1]: Failed to start Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Unit entered failed state.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Failed with result 'exit-code'.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Service hold-off time over, scheduling restart.
Oct 11 21:34:45 kingcolour systemd[1]: Stopped Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: Starting Minecraft Server: private...
Oct 11 21:34:45 kingcolour systemd[1]: Started Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Control process exited, code=exited status=1
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Unit entered failed state.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Failed with result 'exit-code'.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Service hold-off time over, scheduling restart.
Oct 11 21:34:45 kingcolour systemd[1]: Stopped Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Start request repeated too quickly.
Oct 11 21:34:45 kingcolour systemd[1]: Failed to start Minecraft Server: private.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Unit entered failed state.
Oct 11 21:34:45 kingcolour systemd[1]: [email protected]: Failed with result 'exit-code'.

由于 tmux 会吞掉所有可能发生的错误消息,所以我不知道可能出了什么问题。有人遇到过这种情况吗?

答案1

systemd 希望服务保持在前台运行以便对其进行监控。当您使用 运行 tmux detached 时-d,它会分叉一个新进程并返回原始命令。我尚未使用 tmux 和 minecraft 确认这一点,但请尝试Type=forking在部分中设置Service以告诉 systemd 跟踪从 中指定的命令分叉的子进程ExecStart

附注:你确定这里需要 tmux 吗?没有的话会简单得多。

相关内容