A multi-server service for systemd that utilizes GNU Screen to communicate with the server's console.
You would need to have this stuff already present in your system:
- GNU Bash or compatible shell.
- GNU Screen.
- A headless Java runtime.
- Obviously a server jar.
The script requires a user named minecraft to exist in the system. The home directory must be /srv/minecraft/.
NOTE: For some reason Screen refuses to run correctly if this user is using nologin/false instead of an actual shell: needs to be fixed.
sudo mkdir -p /srv/minecraft/
useradd -s /bin/bash -d /srv/minecraft/ minecraft
sudo mkdir -p /srv/minecraft/screen/
Only two files are required to be copied at their destinations:
minecraft@.service
should be copied to /etc/systemd/system/service.sh
should be copied to /srv/minecraft/environment.conf
may be copied to /srv/minecraft/servername/
NOTE: the service.sh script must also be executable:
sudo chmod +x /srv/minecraft/service.sh
- Create a new directory
sudo mkdir -p /opt/minecraft/servername/
- Copy the server jar to it.
The server jar may be vanilla server or patched one - the script literally doesn't care about it. But the jar itself must be named as /srv/minecraft/servername/server.jarsudo cp whatever.jar /opt/minecraft/servername/server.jar
- Start the server for the first time, letting the patched versions to patch themselves and create the eula.txt
sudo systemctl start minecraft@servername
- Accept the EULA by changing the eula property to true
sudo vim /opt/minecraft/servername/eula.txt # ... some evil ViM actions go here, then :wq
- Optionally modify the server.properties
sudo vim /opt/minecraft/servername/server.properties # ... some evil ViM actions go here, then :wq
- Make sure that the owner of all files in the directory is set to minecraft. Otherwise the server may fail to basically create some files and write logs to the disk.
sudo chown -R minecraft /opt/minecraft/
- Start and optionally enable the server service
sudo systemctl start minecraft@servername sudo systemctl enable minecraft@servername
- When you get tired, stop the service
sudo systemctl stop minecraft@servername
- 30-second shutdown timeout that will force systemd to wait until the server is safely stopped instead of rude SIGTERM and non-successful return code
- fix screen not being able to write to /run/screens/
- fix screen not being able to run when the shell is /sbin/nologin
- make screen to redirect standard output to the journal somehow
- probably use title and subtitle commands when the server is scheduled to stop.
- maybe add support for pre-run scripts so I can change MOTD every server restart without any third-party plugins that probably eat the already poor performance.
I have been researching though a lot of projects to figure out why don't my script work as intended. I even tried to use tmux instead of screen but as it turns out, tmux refuses to work without a config or something so I considered using tmux as a cringy way.
So anyway, here's some useful resources:
- minecraft-tmux-service which I tried to use, then gave up and wrote my script looking at this one from time to time to make sure I've done everything correctly
- Minecraft Wiki because god has been dead for a long time
- man systemd, especially systemd.exec and systemd.service