Minecraft Server AutoStart, AutoSleep, and AutoWake

June 6, 2022 5:10 pm

I run a small Minecraft server for my cousins, nieces, nephews, friends, and other family to use. It’s been a fun way to get together and game for all ages during the pandemic across timezones.

While setting up a Minecraft java server is relatively easy – all you need is an Internet connection, java, UEALA agreement, and the know-how to forward a port, I found myself in a situation where I was micromanaging the game. I don’t like keeping things running for days at a time when nobody is using them, so I wanted to have a solution that ‘took care of itself’.

(for latest versions of Minecraft, you need a higher version of Java — I believe 11) Try:

sudo useradd -m -r -d /opt/minecraft minecraft
apt-get install openjdk-17-jre
apt-get install openjdk-17-jdk

Part 1: Set Up a Persistent, Auto-Starting Minecraft Instance

So the first thing I would want to do is set up the server so that it automatically starts when the server starts. I’m doing a lot of things on this server, and Minecraft runs on a separate Ubuntu VM. I want to be able to forget that it is there. As long as the hardware is on, it should be running. For this, we need to establish a systemd service.

Move your Minecraft Server to its own directory: /opt/minecraft/survival.

Create a new user for Minecraft:

sudo useradd -m -r -d /opt/minecraft minecraft
chown -R minecraft /opt/minecraft/survival/

Create a service.

nano /etc/systemd/system/[email protected] 
[Unit]
Description=Minecraft Server: %i
After=network.target

[Service]
WorkingDirectory=/opt/minecraft/%i

User=minecraft
Group=minecraft

Restart=always

ExecStart=/usr/bin/screen -DmS mc-%i /usr/bin/java -Xms1G -Xmx4G -jar minecraft_server.jar nogui

ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN IN 5 SECONDS. SAVING ALL MAPS..."\015'
ExecStop=/bin/sleep 5
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'


[Install]
WantedBy=multi-user.target

You should now be able to use systemctl start minecraft@survival and other systemctl commands to manage the starting and stopping of Minecraft. If you want to provision more servers, just copy the survival folder and change the name; the service will match the new folder name, for example if you create a copy of the survival folder called creative, you will be able to use: systemctl start minecraft@creative. Easy – just be sure to update your ports and such in server.properties for the new instance. Finally, remember to set it to start when the OS starts:

systemctl start minecraft@survival
systemctl enable minecraft@survival

Part 2: Automating Sleep and Wakeup

Note: For the AutoSleep function to work, you must be using a server version that allows for the user of plugins. I use Paper MC: https://papermc.io/downloads

To get your server to automatically shutdown when no users are present, use the plugin mcEmptyServerStopper.

Download the jar:

wget https://github.com/vincss/mcEmptyServerStopper/releases/download/v1.0.1/mcEmptyServerStopper-1.0.1.jar

Copy the jar to the plugins folder.

Start the server, then stop the server. Then navigate to the plugins folder plugins\EmptyServerStopper\ and edit the config.yml to set the amount of time before a shutdown occurs. The time is in minutes. I set mine for 5 minutes.

Now, setup mcSleepingServerStarter.

Simply extract the source files from the zip (make sure you get the version that matches your own), and add all of the files to the root of your server (where the server.jar is).

wget https://github.com/vincss/mcsleepingserverstarter/archive/refs/tags/v1.1.5_1.18.2.tar.gz
tar -xzvf v1.1.5_1.18.2.tar.gz

This is a node.js application. In order for it to work, you will need to install node.js on your server. I believe this is usually installed by default, but if not, just install it using:

wget curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
apt-get install nodejs
apt-get install build-essential
nodejs -v
npm --version

Once nodejs and npm are installed, you need to install your sleeping server starter application.

Navigate to your minecraft root folder and edit sleepingSettings.yml. Edit your ports, make sure your minecraftCommand is correct (java -Xms1G -Xmx4G -jar minecraft_server.jar nogui). This is what mine looks like:

serverName: "Survival Server, waiting for his prince…"

serverPort: 25565
bedrockPort: 19132
loginMessage: "…Waking server up, come back in a minute…"
serverOnlineMode: true # like in the server.properties, to check the licence of player or not.
maxPlayers: 20
favIcon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9i$

webPort: 0 # 0 to disable web hosting, on a positive number a web page it will serve

startMinecraft: 1 # 0 to disable
minecraftCommand: "java -Xms1G -Xmx4G -jar minecraft_server.jar nogui" # or paper.jar or whatever flavor you like

Once this is completed, install the nodejs app.

npm install

Now, in order for the sleeping server to work, Minecraft should not be started as a systemd script. If we do, the server stopper, won’t stop it correctly. This means we should update our script to start the npm wakeup server instead. Let’s try this:

nano /etc/systemd/system/[email protected]
[Unit]
Description=Minecraft Server: %i
After=network.target

[Service]
WorkingDirectory=/opt/minecraft/%i

Restart=always

ExecStart=/usr/bin/screen -DmS mc-%i /usr/bin/npm start
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "quit"\015'

[Install]
WantedBy=multi-user.target

Now you can use systemctl start minecraft@survival to start the npm listener.

systemctl stop minecraft@survival
systemctl start minecraft@survival
systemctl status minecraft@survival

This process changes the Minecraft server to a sleeping Minecraft server. When a user tries to login, they will get a message saying the server is starting. After a few seconds, they can login, craft for a bit, and leave. When all users leave the server, it will automatically shutdown/sleep following the determined duration of time, saving a lot of memory.

Fair warning, if you have any issues getting the above to work, I would check your permissions! I removed the user and group from this startup script because it was causing issues – presumably because the user minecraft did not have permission to run npm scripts. Removing it allows the default system user to take control (I believe – if there are any Linux gurus out there, please clarify in an email). Hope this helps anyone trying to automate a server and save some resources when it is not in use. Enjoy!