Ubuntu 20.04 Minecraft Server Setup - LinuxConfig.org

I copied this into the systemd script, which I’m pasting below. Then below it what happens when I try to shutdown minecraft with the “sudo systemctl stop minecraft@survival”. Then everytime I do a “status” check after the shutdown my console doesn’t seem to understand what’s going on. It doesn’t stop minecraft at all until I reboot the server.

[Unit]
Description=Minecraft Server: %i
After=network.target

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

User=**********
Group=*********

Restart=always

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

ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval ‘stuff “say SERVER WILL SHUT DOWN IN 30 SECONDS. MAKE SURE YOU ARE IN A SAFE SPOT! SAVING ALL MAPS…”\015’
ExecStop=/bin/sleep 20
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval ‘stuff “say SERVER SHUTTING DOWN IN 10 SECONDS. YOU BETTER HURRY UP NOW! SAVING ALL MAPS…”\015’
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval ‘stuff “say 5…”\015’
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval ‘stuff “say 4…”\015’
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval ‘stuff “say 3…”\015’
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval ‘stuff “say 2…”\015’
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval ‘stuff “say 1…”\015’
ExecStop=/bin/sleep 1
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

*********@linuxserver:~$ sudo systemctl status minecraft@survival
● minecraft@survival.service - Minecraft Server: survival
Loaded: loaded (/etc/systemd/system/minecraft@.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2021-02-02 14:27:08 UTC; 1min 30s ago
Main PID: 5079 (screen)
Tasks: 35 (limit: 9290)
Memory: 2.3G
CGroup: /system.slice/system-minecraft.slice/minecraft@survival.service
├─5079 /usr/bin/SCREEN -DmS mc-survival /usr/bin/java -Xmx4G -jar minecraft_server.jar nogui
└─5080 /usr/bin/java -Xmx4G -jar minecraft_server.jar nogui

Feb 02 14:27:08 linuxserver systemd[1]: Started Minecraft Server: survival.
*********@linuxserver:~ sudo systemctl stop minecraft@survival *********@linuxserver:~ sudo systemctl status minecraft@survival
● minecraft@survival.service - Minecraft Server: survival
Loaded: loaded (/etc/systemd/system/minecraft@.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2021-02-02 14:33:36 UTC; 12s ago
Process: 5079 ExecStart=/usr/bin/screen -DmS mc-survival /usr/bin/java -Xmx4G -jar minecraft_server.jar nogui (>
Process: 5891 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff "say SERVER WILL SHUT DOWN IN 30 SECON>
Process: 5892 ExecStop=/bin/sleep 20 (code=exited, status=0/SUCCESS)
Process: 5916 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff "say SERVER SHUTTING DOWN IN 10 SECOND>
Process: 5917 ExecStop=/bin/sleep 5 (code=exited, status=0/SUCCESS)
Process: 5923 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff “say 5…”^M (code=exited, status=0/SU>
Process: 5924 ExecStop=/bin/sleep 1 (code=exited, status=0/SUCCESS)
Process: 5926 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff “say 4…”^M (code=exited, status=0/SU>
Process: 5927 ExecStop=/bin/sleep 1 (code=exited, status=0/SUCCESS)
Process: 5930 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff “say 3…”^M (code=exited, status=0/SU>
Process: 5931 ExecStop=/bin/sleep 1 (code=exited, status=0/SUCCESS)
Process: 5933 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff “say 2…”^M (code=exited, status=0/SU>
Process: 5934 ExecStop=/bin/sleep 1 (code=exited, status=0/SUCCESS)
Process: 5936 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff “say 1…”^M (code=exited, status=0/SU>
Process: 5937 ExecStop=/bin/sleep 1 (code=exited, status=0/SUCCESS)
Process: 5939 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff “save-all”^M (code=exited, status=0/SU>
Process: 5940 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff “stop”^M (code=exited, status=0/SUCCES>
Main PID: 5079 (code=exited, status=1/FAILURE)

Feb 02 14:27:08 linuxserver systemd[1]: Started Minecraft Server: survival.
Feb 02 14:33:03 linuxserver systemd[1]: Stopping Minecraft Server: survival…
Feb 02 14:33:33 linuxserver systemd[1]: minecraft@survival.service: Main process exited, code=exited, status=1/FAIL>
Feb 02 14:33:36 linuxserver systemd[1]: minecraft@survival.service: Failed with result ‘exit-code’.
Feb 02 14:33:36 linuxserver systemd[1]: Stopped Minecraft Server: survival.
lines 1-27/27 (END)

Hi Thencguy,

Welcome to our forums.

I would suggest commenting out all the “ExecStop” lines in your systemd unit file except for the last one, and check if the problem persists.

I commented out all but the last “ExecStop” and this was the result:
Feb 03 01:53:51 linuxserver systemd[1]: Started Minecraft Server: survival.
Feb 09 14:24:39 linuxserver systemd[1]: Stopping Minecraft Server: survival…
Feb 09 14:24:40 linuxserver systemd[1]: minecraft@survival.service: Main process exited, code=exited, status=1/FAILURE
Feb 09 14:24:43 linuxserver systemd[1]: minecraft@survival.service: Failed with result ‘exit-code’.
Feb 09 14:24:43 linuxserver systemd[1]: Stopped Minecraft Server: survival.
scott@linuxserver:/opt/minecraft/survival$ sudo systemctl status minecraft@survival
● minecraft@survival.service - Minecraft Server: survival
Loaded: loaded (/etc/systemd/system/minecraft@.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2021-02-09 14:24:43 UTC; 52s ago
Process: 4087 ExecStart=/usr/bin/screen -DmS mc-survival /usr/bin/java -Xmx4G -jar minecraft_server.jar nogui (code=exited, status=1/FAILURE)
Process: 964456 ExecStop=/usr/bin/screen -p 0 -S mc-survival -X eval stuff “stop”^M (code=exited, status=0/SUCCESS)
Main PID: 4087 (code=exited, status=1/FAILURE)

Feb 03 01:53:51 linuxserver systemd[1]: Started Minecraft Server: survival.
Feb 09 14:24:39 linuxserver systemd[1]: Stopping Minecraft Server: survival…
Feb 09 14:24:40 linuxserver systemd[1]: minecraft@survival.service: Main process exited, code=exited, status=1/FAILURE
Feb 09 14:24:43 linuxserver systemd[1]: minecraft@survival.service: Failed with result ‘exit-code’.
Feb 09 14:24:43 linuxserver systemd[1]: Stopped Minecraft Server: survival.

On my screen it highlights the ^M after the “stop”, this probably means something.

Did you copy-pasted the unit file’s content from the browser? If so, check for any additional characters at the end of the lines that may be interpreted wrongly. I would suggest midnight commander’s editor that shows them visually.

Hi is there a way to turn online mode to false and if yes how?

It only showed CR and LF at the end of each line. I tried deleting these and it just put text lines as continuous lines. So I it doesn’t appear to have anything extra.

you can change that by editing the server.properties file. You have to stop the server before it allows editing.

Try to remove the last few characters of the stop command, so your line ends like the original:

ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval ‘stuff “stop”5’

I commented out my only ExecStop line and retyped it manually as you suggested and it get the same error as before. Failed with result ‘exit-code’

As a side note, it will allow me to “restart” the minecraft server and the status looks fine. I’m about to give up on this install and try another method, my server is already corrupted to the point they can’t interact with certain objects.

Hi. i am pretty new. I wanted to ask if it is possible to start a forge server with that? When yes how? i tried changing the name in the .service file but it doesn’t want to work.
Please help!

I know its a bit old now, really good guide/setup though. Just one thing re failed exit-code errors people are getting (I also had this…). Try running manually with the following:

$ sudo su - minecraft
$ cd /opt/minecraft/survival
$ java -Xms1024M -Xmx4G -jar minecraft_server.jar

I get error java.lang.UnsupportedClassVersionError… blah blah, basically, server.jar was compiled using a later jdk than the one installed of version 11 ($ java -version). I just installed latest jdk from oracle JDK 16.0.1 then changed that to be the default jdk. This now works fine!.

Hope it helps someone out there…

1 Like

This helped me, it was exactly what was wrong on my install.
I used the directions I googled to add to add the apt repository and then install java 16 as default.
*I’d include the link but I’m too new so I can’t.

Thanks for the direction, and to the OP for the amazing post!

How to upgrade from 1.16 to 1.8

Im a year late to help you but I had the same problem so commenting to help the next guy.

Remove “Restart=always” from the script to see the reason it’s restarting.

The error I received was “screen[22658]: No screen session found”

If this is the same for you, my root cause was Minecraft 1.17 requires java 16 and this tutorial installs v.11.

I ran sudo apt install openjdk-16-jdk (probably should have added “headless”) and it ran just fine.

I followed a couple of similar tutorials and made some changes, here is my share

[Unit]
Description=Minecraft Server %i
After=network.target

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

PrivateUsers=true
# Users Database is not available for within the unit, only root and minecraft is available, everybody else is nobody

User=minecraft
Group=minecraft

ProtectSystem=full
# Read only mapping of /usr /boot and /etc

ProtectHome=true
# /home, /root and /run/user seem to be empty from within the unit. It is recommended to enable this setting for all long-running >

ProtectKernelTunables=true
# /proc/sys, /sys, /proc/sysrq-trigger, /proc/latency_stats, /proc/acpi, /proc/timer_stats, /proc/fs and /proc/irq will be read-on>
# Implies MountFlags=slave

ProtectKernelModules=true
# Block module system calls, also /usr/lib/modules. It is recommended to turn this on for most services that do not need special f>
# Implies NoNewPrivileges=yes

ProtectControlGroups=true
# It is hence recommended to turn this on for most services.
# Implies MountAPIVFS=yes

# Set default memory values
Environment="MCMINMEM=512M" "MCMAXMEM=1024M" "SHUTDOWN_DELAY=5" "POST_SHUTDOWN_DELAY=10"
# Change memory values in environment file
EnvironmentFile=-/opt/minecraft/%i/server.conf

# Uncomment this to fix screen on RHEL 8
#ExecStartPre=+/bin/sh -c 'chmod 777 /run/screen'

ExecStart=/bin/sh -c \
    'find -L . \
      -maxdepth 1 \
      -type f \
      -iregex ".*/\\(FTBServer\\|craftbukkit\\|spigot\\|paper\\|forge\\|minecraft_server\\).*jar" \
      -print0 \
      -quit \
    | xargs -0 -I{} \
      /usr/bin/screen -DmS mc-%i \
        /usr/bin/java \
          -server \
          -Xms${MCMINMEM} \
          -Xmx${MCMAXMEM} \
          -XX:+UseG1GC \
          -XX:+ParallelRefProcEnabled \
          -XX:MaxGCPauseMillis=200 \
          -XX:+UnlockExperimentalVMOptions \
          -XX:+DisableExplicitGC \
          -XX:+AlwaysPreTouch \
          -XX:G1NewSizePercent=30 \
          -XX:G1MaxNewSizePercent=40 \
          -XX:G1HeapRegionSize=8M \
          -XX:G1ReservePercent=20 \
          -XX:G1HeapWastePercent=5 \
          -XX:G1MixedGCCountTarget=4 \
          -XX:InitiatingHeapOccupancyPercent=15 \
          -XX:G1MixedGCLiveThresholdPercent=90 \
          -XX:G1RSetUpdatingPauseTimePercent=5 \
          -XX:SurvivorRatio=32 \
          -XX:+PerfDisableSharedMem \
          -XX:MaxTenuringThreshold=1 \
          -Dusing.aikars.flags=https://mcflags.emc.gs \
          -Daikars.new.flags=true \
          -jar {} \
          nogui'

ExecReload=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "reload"\\015'

ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN. Saving map..."\\015'
ExecStop=/bin/sh -c '/bin/sleep ${SHUTDOWN_DELAY}'
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'
ExecStop=/bin/sh -c '/bin/sleep ${POST_SHUTDOWN_DELAY}'

Restart=on-failure
RestartSec=60s

[Install]
WantedBy=multi-user.target

#########
# HowTo
#########
#
# Create a directory in /opt/minecraft/XX where XX is a name like 'survival'
# Add minecraft_server.jar into dir with other conf files for minecraft server
#
# Enable/Start systemd service
#    systemctl enable minecraft@survival
#    systemctl start minecraft@survival
#
# To run multiple servers simply create a new dir structure and enable/start it
#    systemctl enable minecraft@creative
# systemctl start minecraft@creative
#
# To change specific server memory assignment, create file /opt/minecraft/XX/server.conf (where XX is your server name) and add be>
# MCMINMEM=512M
# MCMAXMEM=2048M

Also you can add this function to your .bashrc for an easy screen attach

function minecraft() {
    sudo su -c 'screen -r mc-$1' minecraft
}

Also an alias to give the right perms to the files

alias mcPerms="sudo chown -R minecraft:minecraft /opt/minecraft/*"

And last but not least you can use a samba share for easier server management, I personally use this config

[Minecraft]
    comment = Minecraft server folder
    path = /opt/minecraft
    read only = no
    browsable = yes
    writable = yes
    valid users = @minecraft
    force user = minecraft
    create mask = 0644
    directory mask = 0775

i followed a tutoruial on this site about setting up an minecraft server on ubuntu 20.04 but i need to op myself how do i get in the minecraft command line from the console

i cant link the tutorial because of rules but you can find it easily if you search for “how to create a minecraft server on ubuntu 20.04 linuxconfig”

Solved:
You need not put the following in the command line
Sudo su - minecraft
Screen -r