#!/bin/bash # ******** minecraft control script ******** # ******** using systemd + screen ******** # **** configuration **** user=minecraft path=/opt/$user bold="\033[1m" normal="\033[0m" red="\033[31m" green="\033[32m" yellow="\033[33m" # **** start of script **** state() { if [ -r "$path/$2/minecraft_server.jar" ]; then systemctl status $1@$2 > /dev/null if [ $? -eq 0 ]; then return 0 else return 1 fi else return 2 fi } usage() { echo -e "usage: mc option \n\n ${bold}Servers:${normal}" servers=$(ls -l $path | grep '^d' | awk '{print $9}') for server in $servers; do state $user $server if [ $? -eq 0 ]; then echo -e " [${green}✓${normal}] $server" else echo -e " [${red}✗${normal}] $server" fi done echo -e "\n ${bold}Options:${normal} help Print usage message status Print server(s) start Start server instance stop Stop server instance console Connect to server console\n" } case "$1" in h|help|-h|--help|status|a|-a|--status|l|ls|list|-l|--list) usage ;; s|start|-s|--start) if [ $2 ]; then state $user $2 value=$? if [ $value -eq 0 ]; then echo -e "\n [${yellow}☀${normal}] Server already up.($2)\n" elif [ $value -eq 1 ]; then echo -e "\n [${green}✓${normal}] Starting server.($2)\n" sudo systemctl restart $user@$2 else echo -e "\n [${red}✗${normal}] Server does not exist.($2)\n" exit 1 fi else echo -e "\n [${red}✗${normal}] No server given.\n" exit 1 fi ;; t|stop|-t|--stop) if [ $2 ]; then state $user $2 value=$? if [ $value -eq 0 ]; then echo -e "\n [${yellow}☀${normal}] Stopping server. Sending 10 second notice.($2)" sudo systemctl stop $user@$2 echo -e " [${green}✓${normal}] Stopped server.($2)\n" elif [ $value -eq 1 ]; then echo -e "\n [${yellow}☀${normal}] Server already down.($2)\n" else echo -e "\n [${red}✗${normal}] Server does not exist.($2)\n" exit 1 fi else echo -e "\n [${red}✗${normal}] No server given.\n" exit 1 fi ;; c|console|-c|--console) if [ $2 ]; then state $user $2 value=$? if [ $value -eq 0 ]; then echo -e "\n [${yellow}☀${normal}] Connecting to server console on $user@$2." echo -e " [${yellow}☀${normal}] Exit console with: ${bold}Strg-a d${normal}\n"; sleep 5 sudo -u $user /usr/bin/screen -R mc-$2 elif [ $value -eq 1 ]; then echo -e "\n [${red}✗${normal}] Server not up.($2)\n" exit 1 else echo -e "\n [${red}✗${normal}] Server does not exist.($2)\n" exit 1 fi else echo -e "\n [${red}✗${normal}] No server given.\n" exit 1 fi exit 0 ;; *) usage exit 1 ;; esac exit 0 # **** end of script ****