Skip to content

Instantly share code, notes, and snippets.

@dotStart
Last active March 28, 2024 12:39
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dotStart/ea0455714a0942474635 to your computer and use it in GitHub Desktop.
Save dotStart/ea0455714a0942474635 to your computer and use it in GitHub Desktop.
Systemd services for Minecraft Servers
[Unit]
Description=Minecraft Proxy
Wants=network-online.target
After=network-online.target
# If you want to make sure that your servers start along with the proxy, you can append them to the Wants
# parameter. For example:
# Wants=network-online.target lobby.service survival.service
[Service]
# Ensure to set the correct user and working directory (installation directory of your server) here
User=bungeecord
WorkingDirectory=/opt/bungeecord/
# You can customize the maximum amount of memory as well as the JVM flags here
ExecStart=/usr/bin/java -XX:MaxDirectMemorySize=1G -XX:+UseG1GC -Xmx512M -jar BungeeCord.jar --noconsole
# Restart the server when it is stopped or crashed after 30 seconds
# Comment out RestartSec if you want to restart immediately
Restart=always
RestartSec=30
# Alternative: Restart the server only when it stops regularly
# Restart=on-success
StandardInput=null
[Install]
WantedBy=multi-user.target
[Unit]
Description=Minecraft Server
Wants=network-online.target
After=network-online.target
[Service]
# Ensure to set the correct user and working directory (installation directory of your server) here
User=minecraft
WorkingDirectory=/opt/minecraft
# You can customize the maximum amount of memory as well as the JVM flags here
ExecStart=/usr/bin/java -XX:+UseG1GC -Xmx3G -jar server.jar --nojline --noconsole
# Restart the server when it is stopped or crashed after 30 seconds
# Comment out RestartSec if you want to restart immediately
Restart=always
RestartSec=30
# Alternative: Restart the server only when it stops regularly
# Restart=on-success
# Do not remove this!
StandardInput=null
[Install]
WantedBy=multi-user.target bungeecord.service
[Unit]
Description=Minecraft Server
Wants=network-online.target
After=network-online.target
[Service]
# Ensure to set the correct user and working directory (installation directory of your server) here
User=minecraft
WorkingDirectory=/opt/minecraft
# You can customize the maximum amount of memory as well as the JVM flags here
ExecStart=/usr/bin/java -XX:+UseG1GC -Xmx3G -jar server.jar --nojline --noconsole
# Restart the server when it is stopped or crashed after 30 seconds
# Comment out RestartSec if you want to restart immediately
Restart=always
RestartSec=30
# Alternative: Restart the server only when it stops regularly
# Restart=on-success
# Do not remove this!
StandardInput=null
[Install]
WantedBy=multi-user.target

Minecraft systemd Services

This gist contains service descriptors which may be used to automatically start and re-start Minecraft servers using systemd. This allows proper control of your server startup on modern Linux distributions and will automatically manage all required tasks on startup for you.

Requirements

  • A Linux distribution with systemd support
  • A bit of patience

Caveats

These scripts do not expect any user interaction with the server and thus may not be feasible for you. If you wish to interact with the server you may extend the ExecStart properties with a command like screen or tmux. Please refer to other guides for more information on how to setup these tools.

Simple Setup

Note: Not for users of BungeeCord. Single server setups only!

  1. Place the minecraft.service file in your /etc/systemd/system/ directory
  2. Create a user called minecraft with its home pointed to /opt/minecraft
  3. Customize the file according to your requirements as documented within the file
  4. Install the vanilla server or Spigot in /opt/minecraft
  5. (Optional) Enable the service using systemctl enable minecraft
  6. Start the service using systemctl start minecraft

While the step of enabling the service is optional, you should do so to automatically start the server when the machine reboots.

BungeeCord Setup

  1. Place the bungeecord.service file in your /etc/systemd/system/ directory
  2. Create a user called bungeecord with its home pointed to /opt/bungeecord
  3. Customize the file according to your requirements as documented within the file
  4. Install BungeeCord in /opt/bungeecord/
  5. (Optional) Enable the service using systemctl enable bungeecord

For every server you want to add repeat these steps:

  1. Copy the minecraft-bungeecord.service file in your /etc/systemd/system/ directory and give it a descriptive name (lobby.service for example)
  2. Create a user for the server (using the same name as the service is recommended), also give it a dedicated home directory
  3. Customize the file according to your requirements as documented within the file
  4. Install the vanilla server or Spigot in the home directory
  5. (Optional) Add the server to BungeeCord's dependencies in /etc/systemd/systemctl/bungeecord.service

Note: The dependency approach will automatically start your servers when BungeeCord starts and thus you will only need to enable/start the bungeecord service.

@ChopsKingsland
Copy link

I modified the working directory slightly to be /opt/minecraft/server, and I also updated that in the minecraft.service file. It is also still called server.jar. Here is what an ls of /opt/minecraft/server has in it:

image

@JustZhenya
Copy link

Hello, just want to know, what benefits of StandardInput=null? My paper & velocity servers work fine without it. Does it increase performance or security?

@JustZhenya
Copy link

@dotStart
Copy link
Author

dotStart commented Jul 7, 2023

The background on StandardInput=null back in the day was that it would essentially cause the server to always disable the interactive command line (since it would not be reachable anyway) which would also definitively sort out any issues with log formatting.

It doesn't improve performance but it's one less resource for systemd to allocate to your processes especially if it is never actually used.

TL;DR: Not strictly required but simply good practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment