Skip to content

Instantly share code, notes, and snippets.

@dongalor
Forked from dotStart/MinecraftService.md
Created January 14, 2023 11:53
Show Gist options
  • Save dongalor/4e5b77c509ef6cd6edbb6c6b48a768c3 to your computer and use it in GitHub Desktop.
Save dongalor/4e5b77c509ef6cd6edbb6c6b48a768c3 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.

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