无法使用 systemd 启动正在运行的 minecraft 服务器(“未找到屏幕会话”)

无法使用 systemd 启动正在运行的 minecraft 服务器(“未找到屏幕会话”)

我在家用服务器上运行 ubuntu 20.04 服务器,并且我有一个自托管的 minecraft 实例,位于 /opt/minecraft/server,由用户 minecraft 拥有。我可以手动激活服务器。但是,我想让服务器持续运行该进程并在启动时运行,但我无法让 systemd 脚本正常工作。我将在下面发布,但这里有一些准备工作

这是我验证 minecraft:minecraft 拥有一切:

albert@mfs:/opt/minecraft$ ls -l survival
total 37108
-rw-r--r--  1 minecraft minecraft        2 Nov 26 01:04 banned-ips.json
-rw-r--r--  1 minecraft minecraft        2 Nov 26 01:04 banned-players.json
-rw-r--r--  1 minecraft minecraft      180 Nov 25 23:16 eula.txt
drwxr-xr-x  2 minecraft minecraft     4096 Nov 26 01:04 logs
-rw-r--r--  1 minecraft minecraft      136 Nov 26 01:04 ops.json
-rw-r--r--  1 minecraft minecraft 37961464 Nov 25 23:16 server.jar
-rw-r--r--  1 minecraft minecraft     1073 Nov 26 01:04 server.properties
-rw-r--r--  1 minecraft minecraft      323 Nov 26 03:30 usercache.json
-rw-r--r--  1 minecraft minecraft        2 Nov 25 23:16 whitelist.json
drwxr-xr-x 11 minecraft minecraft     4096 Nov 26 04:36 world

启动服务并检查状态后:

albert@mfs:~$ sudo systemctl status minecraft@survival[email protected] - Minecraft Server survival
     Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: enabled)
     Active: activating (auto-restart) (Result: exit-code) since Thu 2020-11-26 21:13:11 UTC; 5s ago
    Process: 99802 ExecStart=/bin/sh -c /usr/bin/screen -DmS mc-survival /usr/bin/java -server -Xms512M -Xmx2048M -XX:+UseG1GC -XX:+CMSIncrementalPacing -XX:+CMSClassUnloadi>
    Process: 99820 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff "say SERVER SHUTTING DOWN. Saving map..."\015 (code=exited, status=1/FAILURE)
   Main PID: 99802 (code=exited, status=0/SUCCESS)

检查 journalctl 之后

    albert@mfs:~$ journalctl -u minecraft@survival
    -- Logs begin at Wed 2020-06-03 18:13:36 UTC, end at Thu 2020-11-26 20:59:45 UTC. --
    Nov 25 23:18:34 my.freakin.server systemd[1]: Started Minecraft Server: survival.
    Nov 25 23:18:34 my.freakin.server screen[78218]: No screen session found.
    Nov 25 23:18:34 my.freakin.server systemd[1]: [email protected]: Control process exited, code=exited, status=1/FAILURE
    Nov 25 23:18:34 my.freakin.server systemd[1]: [email protected]: Failed with result 'exit-code'.
    Nov 25 23:18:34 my.freakin.server systemd[1]: [email protected]: Scheduled restart job, restart counter is at 1. 
    
    [and so on, four more times]

我尝试了几个不同的配置文件,但发现这个问题没有任何变化(可以确认我已经进行了守护进程重新加载)。(我应该启用 Forking 吗?)。这是我当前的 systemd 文件:

albert@mfs:~$ cat /etc/systemd/system/[email protected]    
# source: https://github.com/agowa338/MinecraftSystemdUnit/
# License: MIT
[Unit]
Description=Minecraft Server %i
After=network.target

[Service]
WorkingDirectory=/opt/minecraft/%i
PrivateUsers=true 
# Users Database is not available for within the unit, only root and minecraft is available, everybody else is nobody
User=minecraft
Group=minecraft
ProtectSystem=full 
# Read only mapping of /usr /boot and /etc
ProtectHome=true 
# /home, /root and /run/user seem to be empty from within the unit. It is recommended to enable this setting for all long-running services (in particular network-facing ones).
ProtectKernelTunables=true 
# /proc/sys, /sys, /proc/sysrq-trigger, /proc/latency_stats, /proc/acpi, /proc/timer_stats, /proc/fs and /proc/irq will be read-only within the unit. It is recommended to turn this on for most services.
# Implies MountFlags=slave
ProtectKernelModules=true 
# Block module system calls, also /usr/lib/modules. It is recommended to turn this on for most services that do not need special file systems or extra kernel modules to work
# Implies NoNewPrivileges=yes
ProtectControlGroups=true 
# It is hence recommended to turn this on for most services.
# Implies MountAPIVFS=yes
# ExecStart=/bin/strace -o /tmp/s -f screen -dm -S me bash -c 'sleep 999'
ExecStart=/bin/sh -c '/usr/bin/screen -DmS mc-%i /usr/bin/java -server -Xms512M -Xmx2048M -XX:+UseG1GC -XX:+CMSIncrementalPacing -XX:+CMSClassUnloadingEnabled -XX:ParallelGCThreads=2 -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -jar $(ls -v | grep -i "FTBServer.*jar\|minecraft_server.*jar" | head -n 1) nogui'

#ExecReload=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "reload"\\015'

ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN. Saving map..."\\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"\\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\\015'
ExecStop=/bin/sleep 10

Restart=on-failure
RestartSec=60s

[Install]
WantedBy=multi-user.target

#########
# HowTo
#########
#
# Create a directory in /opt/minecraft/XX where XX is a name like 'survival'
# Add minecraft_server.jar into dir with other conf files for minecraft server
#
# Enable/Start systemd service
#    systemctl enable minecraft@survival
#    systemctl start minecraft@survival
#
# To run multiple servers simply create a new dir structure and enable/start it
#    systemctl enable minecraft@creative
# systemctl start minecraft@creative

在 ExecStart 命令的正上方,您可以看到一个注释掉的命令,上面有一个 strace。我做了足够的研究来弄清楚如何将它放进去,但我几乎无法阅读它。跟踪很长,所以除非有人要求,否则我不会添加它;话虽如此,这是引发某种错误的跟踪的所有结果。

81270 arch_prctl(0x3001 /* ARCH_??? */, 0x7ffc8a212000) = -1 EINVAL (Invalid argument)
81270 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
81270 access("/opt/minecraft/.nethackrc", F_OK) = -1 ENOENT (No such file or directory)
81270 access("/opt/minecraft/.nethackrc", F_OK) = -1 ENOENT (No such file or directory)
81270 connect(3, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
81270 connect(3, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
81270 openat(AT_FDCWD, "/etc/shadow", O_RDONLY|O_CLOEXEC) = -1 EACCES (Permission denied)
81270 openat(AT_FDCWD, "/etc/shadow", O_RDONLY|O_CLOEXEC) = -1 EACCES (Permission denied)
81271 connect(3, {sa_family=AF_UNIX, sun_path="/run/screen/S-minecraft/81271.me"}, 34) = -1 ENOENT (No such file or directory)
81271 unlink("/run/screen/S-minecraft/81271.me") = -1 ENOENT (No such file or directory)
81271 ioctl(1, TCGETS, 0x7ffc8a210800)  = -1 ENOTTY (Inappropriate ioctl for device)
81271 stat("/opt/minecraft/.terminfo", 0x564c0619b900) = -1 ENOENT (No such file or directory)
81271 access("/etc/terminfo/s/screen", R_OK) = -1 ENOENT (No such file or directory)
81271 ioctl(2, TCGETS, 0x7ffc8a210800)  = -1 ENOTTY (Inappropriate ioctl for device)
81271 ioctl(2, TCGETS, 0x7ffc8a210790)  = -1 ENOTTY (Inappropriate ioctl for device)
81271 openat(AT_FDCWD, "/opt/minecraft/.screenrc", O_RDONLY) = -1 ENOENT (No such file or directory)
81271 stat("bash", 0x7ffc8a2107f0)      = -1 ENOENT (No such file or directory)
81271 openat(AT_FDCWD, "/dev/ptmx", O_RDWR) = 4
81271 connect(5, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
81271 connect(5, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
81271 chown("/dev/pts/1", 995, 5)       = -1 EINVAL (Invalid argument)
81272 access("/etc/terminfo/s/screen", R_OK) = -1 ENOENT (No such file or directory)
81272 execve("/usr/local/sbin/bash", ["bash", "-c", "sleep 999"], 0x564c0619ff10 /* 12 vars */) = -1 ENOENT (No such file or directory)
81272 execve("/usr/local/bin/bash", ["bash", "-c", "sleep 999"], 0x564c0619ff10 /* 12 vars */) = -1 ENOENT (No such file or directory)
81272 execve("/usr/sbin/bash", ["bash", "-c", "sleep 999"], 0x564c0619ff10 /* 12 vars */) = -1 ENOENT (No such file or directory)
81272 arch_prctl(0x3001 /* ARCH_??? */, 0x7ffede633760) = -1 EINVAL (Invalid argument)
81272 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
81272 ioctl(-1, TIOCGPGRP, 0x7ffede6335b4) = -1 EBADF (Bad file descriptor)
81272 stat("/usr/local/sbin/bash", 0x7ffede633220) = -1 ENOENT (No such file or directory)
81272 stat("/usr/local/bin/bash", 0x7ffede633220) = -1 ENOENT (No such file or directory)
81272 stat("/usr/sbin/bash", 0x7ffede633220) = -1 ENOENT (No such file or directory)
81272 getpeername(0, 0x7ffede6335b0, [16]) = -1 ENOTSOCK (Socket operation on non-socket)
81272 stat("/usr/local/sbin/sleep", 0x7ffede633110) = -1 ENOENT (No such file or directory)
81272 stat("/usr/local/bin/sleep", 0x7ffede633110) = -1 ENOENT (No such file or directory)
81272 stat("/usr/sbin/sleep", 0x7ffede633110) = -1 ENOENT (No such file or directory)
81272 arch_prctl(0x3001 /* ARCH_??? */, 0x7ffc09151430) = -1 EINVAL (Invalid argument)
81272 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)

有人有什么想法吗?我不清楚屏幕会话问题是什么,也不清楚如何解决它。非常感谢大家的时间和想法。

答案1

在您的 ExecStart 行上,

  • 该服务正在寻找$(ls -v | grep -i "FTBServer.*jar\|minecraft_server.*jar" | head -n 1)

  • 但是服务器名称是server.jar...grep 无法找到它。

一个简单的解决方案就是用简单的真实文件名替换那个长表达式。

相关内容