顺手搭建一个 Minecraft 服务,以前都是直接用脚本控制,现在我们完全可以用 systemd 来管理服务端。
Minecraft 安装前提要素
首先安装必备的 JAVA JDK
yum install java-1.8.0-openjdk-headless screen
然后创建一个用户专门用于 minecraft 服务 注意 Minecraft 多服务器汇总文件夹路径为 /minecraft
useradd -s /usr/sbin/nologin -M -d /minecraft minecraft mkdir /minecraft chown -R minecraft:minecraft /minecraft
相关的 Systemd 服务配置文件参考说明可以访问 [ 链接 ]
涉及 硬件资源控制设置 的 Systemd 说明可以参考文档 [ 链接 ]
涉及 关闭服务器信号 的 Systemd 说明可以参考文档 [ 链接 ]
Minecraft 的通用 Systemd 服务文件
创建通用服务文件 /etc/systemd/system/[email protected] 并输入以下内容。
注意 WorkingDirectory=/minecraft/%i 为你的 Minecraft 服务端文件夹路径,%i 对应进程名的变量。
[Unit] Description=Minecraft Server %i After=network.target [Service] WorkingDirectory=/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/sh -c '/usr/bin/screen -DmS mc-%i /usr/bin/java -server -Xms512M -Xmx2048M -XX:+UseG1GC -XX:+CMSClassUnloadingEnabled -XX:ParallelGCThreads=2 -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -jar $(ls -v | grep -i "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 /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
创建好后,你可以通过这个通用服务文件启动 Minecraft 服务。
例如你的服务端文件夹为 /minecraft/server 此时你的启动命令为
systemctl start minecraft@server
记得如果想设置开机启动同样要配置上
systemctl enable minecraft@server
非服务方式,单独启动服务端文件的命令
/usr/bin/java -server -Xms512M -Xmx2048M -XX:+UseG1GC -XX:+CMSClassUnloadingEnabled -XX:ParallelGCThreads=2 -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -jar minecraft_server.1.14.3.jar nogui
SRV 记录
Name: _minecraft._tcp.mc.yourdomain.com Priority: 10 Weight: 10 Port: 25565 Address: forward.yourdomain.com