Skip to content

Instantly share code, notes, and snippets.

@SpiZeak
Created June 23, 2020 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpiZeak/b138b59149434ee9ee6367c541d9b2c1 to your computer and use it in GitHub Desktop.
Save SpiZeak/b138b59149434ee9ee6367c541d9b2c1 to your computer and use it in GitHub Desktop.
A script for controlling a minecraft server
#!/bin/bash
# Author: Max Trewhitt <max@trewhitt.se>
# Template: https://natelandau.com/boilerplate-shell-script-template/
# Description: A script for controlling a minecraft server
# Config
FILENAME=Paper-latest.jar
SERVER_PATH=/home/mc-server
SERVER_DOWNLOAD_URL=https://yivesmirror.com/files/paper/$FILENAME
# Print usage
usage() {
echo -n "
Minecraft server script.
Usage:
minecraft [ARGUMENT]
[ARGUMENT]:
help Prints this help dialog
status Displays the current status
start Starts the server
stop Stops the server
console Go to the server console
update Updates the minecraft server
"
}
start() {
if ! screen -list | grep -q "mc-server"; then
screen -dm -S mc-server bash -c "(cd $SERVER_PATH && java -Xms1500M -Xmx1500M -jar $FILENAME)"
echo "Server started."
else
echo "Server already running."
fi
}
stop() {
screen -S mc-server -p 0 -X stuff "^Mstop^M"
echo "Server stopped."
}
console() {
screen -r mc-server
}
status() {
if ! screen -list | grep -q "mc-server"; then
echo "Server not running."
else
echo "Server running."
fi
}
update() {
wget -P $SERVER_PATH -N $SERVER_DOWNLOAD_URL
}
# Check if script is run as root
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "This script must be run as root, try again with sudo."
exit
fi
# Print help if no arguments were passed.
# Uncomment to force arguments when invoking the script
[[ $# -eq 0 ]] && set -- "help"
# Read the options and set stuff
case $1 in
help) usage;;
start) start;;
stop) stop;;
console) console;;
status) status;;
update) update;;
*) echo "invalid option: '$1'.";;
esac
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment