4 ms·
What I typically do is create a systemd service for game servers and attach a TTY. That way it starts with the rest of my web services, and linux already handle
by Xelynega 2y ago
What I typically do is create a systemd service for game servers and attach a TTY. That way it starts with the rest of my web services, and linux already handles "i/o to processes" via files that other process can access(e.x. /run/minecraft/{stdin,stdout,stderr})
- wolrah 2y agoWould you mind expanding on that, or can you point me at some relevant documentation? I've never seen a systemd service example for Minecraft which allowed for sending commands to the server CLI and seeing the result without involving screen/tmux/etc. The top result on Google just doesn't allow command input at all, running the service "headless", the one on the official MC wiki uses screen, and the only other options I've seen use RCON which is neither secure nor does it show the responses you'd get on the MC console. If there's a way to run just the straight Minecraft JAR as a background service and still be able to interact with it in the occasional cases where I need to I'm very interested.
- Xelynega 2y agoBasically, you setup the standard minecraft service and then create a "socket" in systemd to use as stdin for the process(relevant documentation in systemd.socket and systemd.exec). For me this looks like -- /etc/systemd/system/minecraft.service -- [Unit] Description="Minecraft server service" [Service] Environment=JAVA_HOME="/usr/lib/jvm/java-22-openjdk" WorkingDirectory=/home/steam/minecraft/1.20.6/ ExecStart=/usr/lib/jvm/java-22-openjdk/bin/java -Xmx4096M -Xms1024M -jar /home/steam/minecraft/1.20.6/server.jar nogui User=steam Group=steam Sockets=minecraft.socket StandardInput=socket StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target -- /etc/systemd/system/minecraft.socket -- [Socket] ListenFIFO=%t/minecraft.stdin Service=minecraft.service ------------- What this will do is add a systemd dependency on minecraft.service to start minecraft.socket first(which creates the fifo `/run/minecraft.stdin`) then setup minecraft.service to listen to this socket for it's StandardInput(while leaving stdout and stderr pointing towards the journal). The service can then be started and set to automatically start on boot(`systemctl daemon-reload && systemctl enable --now minecraft`). While running, data can be written to the socket file with `echo` and redirection(e.x. `echo "help" > /run/minecraft.stdin`), and the output will be visible in the journal(`journalctl -xef --unit minecraft.service`) If you set stderr/out to go over the socket as well, then you can attach something like `screen` to it and use it like a typical TTY(or `telnet`). This uses the file `/run/minecraft.stdin` as the socket, but the documentation for systemd.socket shows that this can also be a TCP port to listen for connections(and systemd.service shows using regular files, but then you have to manually set them up).
- Xelynega 2y agoOh yea one more comment, this stdin redirection isn't really necessary in minecraft from the last decade. The minecraft server has a built-in RCON server running on a separate port than can be enabled(https://wiki.vg/RCON https://wiki.vg/RCON), and once enabled can be interacted with an RCON client(like https://github.com/Tiiffi/mcrcon https://github.com/Tiiffi/mcrcon). So instead of redirecting stdin to a systemd process, you can also just leave stdin disconnected and use the built-in RCON server to do commands every so often.