This is a collection of bash scripts and systemd-units that will allow a Minecraft Server to:
- automatically start when players try to join
- automatically stop after X (default 15min) minutes when no players are online
Since Minecraft does not natively support socket activation there is some trickery involved.
-
Firstly there is the systemd socket mcserv.socket which listenes on Port 25565, if it receives traffic it starts mcserv.service which will:
- stop mcserv.socket (since it conflics)
- start mcserv-stoptimerctrl.service
- start the minecraft server through mcserv-start.sh
-
While the Server is running, mcserv-stoptimerctrl.service will monitor the servers playercount, through reading the serverlog.
-
If the playercount reaches 0 it will start mcserv-stop.timer, which will stop the minecraftserver after x minutes (therefore stopping mcserv-stoptimerctrl.service and restarting mcserv.socket).
-
If the playercount becomes > 0 while the timer is started, the timer is stopped.
- Initially connecting players will experience a connection closed error message, since the mcserv.socket first bound to port 25565 and is then stopped. I don't know of any mechanism to give the open socket to the minecraft server.
Just try joining again after some time when the server is up and running.
MINECRAFT_UID
: the uid of the user that runs minecraft the default is root but you should probably change thatMINECRAFT_GID
: the gid of the user that runs minecraft the default is root but you should probably change thatMINECRAFT_PASSWORD
: the password for the minecraft user (for use with ftp)MINECRAFT_SERVER_JAR
: the filename of the server jarMINECRAFT_JVM_ARGS
: the arguments passed to the java-virtual-machine. Since they are often very long consider unsing a .env file.MINECRAFT_JAR_ARGS
: the arguments passed to the minecraft server jar
25565/tcp
: minecraft25565/udp
: minecraft8000/tcp
: systemd-query-rest21/tcp
: ftp