Skip to content

Instantly share code, notes, and snippets.

@einarjh
Last active June 10, 2018 12:30
Show Gist options
  • Save einarjh/976c9a996cba63ed93f78c81feeec5fd to your computer and use it in GitHub Desktop.
Save einarjh/976c9a996cba63ed93f78c81feeec5fd to your computer and use it in GitHub Desktop.
Simple Minecraft service take 2 (screen based)
# This unit file is suitable for running as a normal user, as a user unit
# I.e. put the file in $HOME/.config/systemd/user and do systemctl --user daemon-reload
# Then start it with systemctl --user start minecraft.service
# Your user needs permission to run units while not logged in (linger):
# sudo systemctl enable-linger <your user>
[Unit]
Description=Minecraft Server
After=network.target
[Service]
Restart=always
RestartSec=5s
# Some variables that we need, change JavaOpts and ServerJar as needed
WorkingDirectory=/home/<your user>/server
Environment="JavaOpts=-Xms2048M -Xmx2048M"
Environment="ServerJar=minecraft_server.jar"
# The command that spins up the server, you should never need to change this, use the environment variables above instead
ExecStart=/usr/bin/screen -DmS minecraft /usr/bin/java $JavaOpts -jar $ServerJar nogui
# Redirect the logs to the journal, as they are invisible to systemd otherwise
ExecStartPost=-/bin/sh -c "tail -n0 -F logs/latest.log | stdbuf -oL cut -d ' ' -f 2- | systemd-cat -t minecraft &"
# Stop the server nicely, with ingame notice
ExecStop=-/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "say The server will shut down in 5 seconds!"\015'
ExecStop=/bin/sleep 5
ExecStop=-/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "save-all"\015'
ExecStop=-/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "stop"\015'
ExecStop=-/bin/bash -c 'while kill -0 "$MAINPID" > /dev/null 2>&1; do sleep 1; done'
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment