#!/bin/bash p="#" a1="=>" a2=" ->" set -euo pipefail # Introduction. clear -x echo "This script is able to: create a user, install Java, download and run an" echo "installer, update an existing server, create a launch script, create a systemd" echo "service unit, and create a shortcut command to run commands directly on the" echo "Minecraft console and view the output." echo "" echo "This script should be run as root on an unused installation of Arch Linux." echo "Before starting, have a directory prepared to store game data." echo "" echo "If you plan to manually install a Java version or use your own server installer," echo "make sure to prepare them before starting this script. In the case of a server" echo "installer, just make sure to know the file path." echo "" echo "It is safe to abort/stop the script via Ctrl+C at any input prompt, and you can" echo "get back to any point just by skipping most steps and redoing a couple." echo "" echo "NOTICE: The Minecraft Fabric installer supports versions from 1.14 to latest." echo " For other Minecraft versions, download the desired server installer file" echo " manually before running this script." echo "NOTICE: The available options for Java are: 8, 11, 17, 21, and 25, or anything" echo " else available from the Arch repositories." echo " For other Java versions, install it manually. The AUR has many." while true; do echo "" read -rp "$p Are you ready to proceed? [Y/n] " proceed case "$proceed" in [Yy]|"" ) break;; [Nn] ) echo "$a1 Exiting."; exit;; * ) echo "$a1 Invalid response.";; esac done # Compatibility tests. echo "" echo "$a1 Running compatibility tests..." test1="" test2="" test3="" test4="" test5="" test6="" tests_failed="" echo -n "$a2 Checking curl... " if curl --version &> /dev/null; then echo "PASS"; else test1="fail"; echo "FAIL"; fi echo -n "$a2 Checking for systemd... " if [ -d "/run/systemd/system" ]; then echo "PASS"; else test2="fail"; echo "FAIL"; fi echo -n "$a2 Checking root privileges... " if [ "$EUID" = "0" ]; then echo "PASS"; else test3="fail"; echo "FAIL"; fi echo -n "$a2 Checking /etc/systemd/system/ permissions... " if touch "/etc/systemd/system/permission.test" &> /dev/null && rm "/etc/systemd/system/permission.test" &> /dev/null; then echo "PASS"; else test4="fail"; echo "FAIL"; fi echo -n "$a2 Checking connection... " if [ "$(curl -o /dev/null -ILsw "%{response_code}" https://archlinux.org/)" -eq 200 ]; then echo "PASS"; else test5="fail"; echo "FAIL"; fi echo -n "$a2 Checking fabricmc.net connection... " if [ "$(curl -o /dev/null -ILsw "%{response_code}" https://maven.fabricmc.net/)" -eq 200 ]; then echo "PASS"; else test6="fail"; echo "FAIL"; fi if [ "$test1" = "fail" ]; then echo "WARNING: curl command error, please skip the Fabric installer." tests_failed="true"; fi if [ "$test2" = "fail" ]; then echo "WARNING: this system is not using systemd, please skip creating unit files." tests_failed="true"; fi if [ "$test3" = "fail" ]; then echo "WARNING: root privileges are not detected, please run again as root." tests_failed="true"; fi if [ "$test4" = "fail" ]; then echo "WARNING: cannot write to systemd directory, please skip creating unit files." tests_failed="true"; fi if [ "$test5" = "fail" ]; then echo "WARNING: cannot connect to archlinux.org, downloads will likely fail. If you can" echo " verify that you can install packages, then it's safe to proceed." tests_failed="true"; fi if [ "$test6" = "fail" ]; then echo "WARNING: cannot connect to maven.fabricmc.net, please skip the Fabric installer." tests_failed="true"; fi if [ "$tests_failed" = "true" ]; then while true; do echo "" read -rp "$p At least one test failed, are you sure you want to proceed? [y/N] " skip_warn case "$skip_warn" in [Yy] ) echo "$a1 Ignoring warnings..."; break;; [Nn]|"" ) echo "$a1 Exiting."; exit;; * ) echo "$a1 Invalid response.";; esac done else echo "" echo "$a1 All tests completed successfully." fi # Create/select a user. while true; do echo "" read -rp "$p Do you want to create a new user? [y/N] " create_user case "$create_user" in [Yy] ) echo "$a1 Creating a user..." echo "" read -rp "$p Enter the user name: " user echo "$a2 Checking if user exists." if id "$user" &> /dev/null; then echo "$a2 User '$user' already exists." continue fi echo "" echo "The home directory must be specified as an absolute file path." echo "Using an asterisk ('*') takes the first match." while true; do echo "" read -rp "$p Enter the home directory path (ENTER for '/home/$user'): " home_dir_pattern if [ ! "$home_dir_pattern" ]; then home_dir_pattern="/home/$user" fi mapfile -t files < <(compgen -G "${home_dir_pattern%/}") home="${files[0]}" if [ ! -d "$home" ]; then home="$home_dir_pattern" echo "$a2 Directory not found: '$home'." while true; do echo "" read -rp "$p Do you want to create this directory? [Y/n] " create_home case "$create_home" in [Yy]|"" ) echo "$a2 Creating home directory: '$home'." mkdir -p "$home" || exit 1 echo "$a2 Selected home directory." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done else echo "$a2 Found directory: '$home'." while true; do echo "" read -rp "$p Do you want to continue with this directory? [Y/n] " use_home case "$use_home" in [Yy]|"" ) echo "$a2 Selected home directory: '$home'." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done fi done echo "$a2 Creating user." if useradd -d "$home" "$user" &> /dev/null; then echo "$a2 Setting ownership." chown -R "$user:$user" "$home" echo "$a2 Set a password for user '$user':" passwd "$user" echo "$a1 Created user '$user' with home directory: '$home'." else echo "$a1 Failed to create user '$user'." fi break;; [Nn]|"" ) echo "$a1 Skipping..."; break;; * ) echo "$a1 Invalid response.";; esac done echo "" echo "This user will own the data directory and will run the Minecraft server (i.e." echo "execute Java). Ensure that the selected user will have the ability to do so." while true; do echo "" read -rp "$p Enter a user: " user echo "$a1 Checking for user '$user'..." if id "$user" &> /dev/null; then echo "$a2 Found user: '$user'." while true; do echo "" read -rp "$p Do you want to continue with this user? [Y/n] " use_user case "$use_user" in [Yy]|"" ) echo "$a1 Selected user '$user'." break 2;; [Nn] ) break;; * ) echo "$a1 Invalid response.";; esac done else echo "$a2 Could not find user: '$user'." fi done # Data directory. echo "" echo "The data directory stores the minecraft directories (minecraft/ and sometimes" echo "minecraft.bak/) and some generated scripts. The minecraft directories store the" echo "server itself and all of the game data." echo "All server files (excluding systemd units and command symlinks) will be within" echo "the specified data directory. This does NOT allow having multiple servers," echo "especially when separate Java versions or systemd units are needed." echo "Running multiple servers would require changing various names, commands, and" echo "file paths, which goes beyond the scope of this script. Instead, using separate" echo "machines via a hypervisor is recommended and is the intended use of this script." echo "" echo "The data directory can be specified as any directory on the system, but must be" echo "specified as an absolute file path. Using a relative file path will cause issues" echo "later on." echo "Using an asterisk ('*') takes the first match." while true; do echo "" read -rp "$p Enter the data directory path: " data_dir_pattern mapfile -t files < <(compgen -G "${data_dir_pattern%/}") data_dir="${files[0]}" if [ ! -d "$data_dir" ]; then data_dir="$data_dir_pattern" echo "$a1 Directory not found: '$data_dir'." while true; do echo "" read -rp "$p Do you want to create this directory? [Y/n] " create_data_dir case "$create_data_dir" in [Yy]|"" ) echo "$a2 Creating data directory: '$data_dir'." mkdir -p "$data_dir" || break echo "$a2 Selected data directory." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done else echo "$a1 Found directory: '$data_dir'." echo "NOTICE: Keep in mind that multiple steps involve removing directories based on" echo " this file path. A malformed or incorrect value could be very bad." while true; do echo "" read -rp "$p Do you want to continue with this directory? [Y/n] " use_dir case "$use_dir" in [Yy]|"" ) echo "$a1 Selected data directory: '$data_dir'." break 2;; [Nn] ) break;; * ) echo "$a1 Invalid response.";; esac done fi done echo "$a2 Setting ownership." chown "$user:$user" "$data_dir" # Install/update a server. echo "" echo "Choose to install a new server or update an existing one." echo "WARNING: Only update an existing server if it was initially installed using this" echo " script, or if you know that it's been set up in the same way." echo "NOTICE: This will replace any existing minecraft.bak/ directory." while true; do echo "" read -rp "$p Update an existing server? [y/N] " update case "$update" in [Yy] ) update="true" echo "$a1 Updating..." break;; [Nn]|"" ) update="false" echo "$a1 Skipping..." break;; * ) echo "$a1 Invalid response.";; esac done echo "$a1 Changing working directory to '$data_dir'." cd "$data_dir" || exit 1 if [ "$update" = "true" ]; then req_failed="" some_failed="" if [ -d "minecraft.bak" ]; then echo -n "$a2 Removing previous minecraft.bak directory... " if rm -rf "minecraft.bak/"; then echo "DONE"; else echo "FAIL"; req_failed="true"; fi fi echo -n "$a2 Backing up current minecraft directory... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif mv "minecraft/" "minecraft.bak/"; then echo "DONE"; else echo "FAIL"; req_failed="true"; fi echo -n "$a2 Creating new minecraft directory... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif mkdir -p "minecraft"; then echo "DONE"; else echo "FAIL"; req_failed="true"; fi echo -n "$a2 Copying world... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif cp -R "minecraft.bak/world/" "minecraft/world/"; then echo "DONE"; else echo "FAIL"; some_failed="true"; fi echo -n "$a2 Copying mods... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif cp -R "minecraft.bak/mods/" "minecraft/mods/"; then echo "DONE"; else echo "FAIL"; some_failed="true"; fi echo -n "$a2 Copying config... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif cp -R "minecraft.bak/config/" "minecraft/config/"; then echo "DONE"; else echo "FAIL"; some_failed="true"; fi echo -n "$a2 Copying defaultconfigs... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif cp -R "minecraft.bak/defaultconfigs/" "minecraft/defaultconfigs/"; then echo "DONE"; else echo "FAIL"; some_failed="true"; fi echo -n "$a2 Copying properties and player/IP lists... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif cp "minecraft.bak/banned-ips.json" "minecraft.bak/banned-players.json" "minecraft.bak/ops.json" "minecraft.bak/server.properties" "minecraft.bak/whitelist.json" "minecraft/"; then echo "DONE"; else echo "FAIL"; some_failed="true"; fi echo -n "$a2 Updating ownership... " if [ "$req_failed" = "true" ]; then echo "SKIPPED" elif chown -R "$user:$user" "minecraft"; then echo "DONE"; else echo "FAIL"; req_failed="true"; fi if [ "$req_failed" = "true" ] || [ "$some_failed" = "true" ]; then while true; do echo "" read -rp "$p At least one step failed, do you want to proceed anyway? [y/N] " skip_fail case "$skip_fail" in [Yy] ) echo "$a1 Ignoring failures..."; break;; [Nn]|"" ) echo "$a1 Exiting. Please try again or resolve failures manually."; exit;; * ) echo "$a1 Invalid response.";; esac done fi echo "$a1 Finished copying data." echo "NOTICE: After completing the installation, please check for other config files" echo " or directories that you may want to keep such as logs, configurations," echo " mod-specific directories, etc." echo "NOTICE: Mods must be updated manually, please do so before starting the server." echo "$a1 Changing working directory to '$data_dir/minecraft'." cd "$data_dir/minecraft" || exit 1 else existing_dir="" echo "$a1 Creating minecraft directory..." if [ -d "minecraft" ]; then echo "$a2 The minecraft directory already exists in: '$data_dir'." echo "" echo "If this directory is already backed up or is no longer needed and you plan to" echo "set up the server again or use a new server, then it should be removed. You will" echo "have a few seconds to stop the script if you choose to remove the directory." while true; do echo "" read -rp "$p Remove directory before continuing? [y/N] " rm_dir case "$rm_dir" in [Yy] ) echo "$a2 Removing: '$data_dir/minecraft' in 10 seconds." sleep 5 echo "$a2 5."; sleep 1 echo "$a2 4."; sleep 1 echo "$a2 3."; sleep 1 echo "$a2 2."; sleep 1 echo "$a2 1."; sleep 1 echo "$a2 Removing minecraft directory." rm -rf "minecraft" || exit 1 break;; [Nn]|"" ) echo "$a2 Using existing minecraft directory." existing_dir="true" break;; * ) echo "$a1 Invalid response.";; esac done fi if [ "$existing_dir" = "true" ]; then echo "$a2 Updating ownership." else echo "$a2 Creating minecraft directory." mkdir -p "$data_dir/minecraft" echo "$a2 Setting ownership." fi chown "$user:$user" "$data_dir/minecraft" echo "$a2 Changing working directory to '$data_dir/minecraft'." cd "$data_dir/minecraft" || exit 1 if [ "$existing_dir" = "true" ]; then echo "$a1 Using minecraft directory." else echo "$a1 Created minecraft directory." fi fi # Install Java. echo "" echo "Choose a valid version of Java." echo "Here is a table of Minecraft versions and their minimum Java requirements:" echo " Minecraft | Java" echo " <= 1.5.2 | 5 (1.5.0)" echo " 1.6.1 - 1.11.2 | 6 (1.6.0)" echo " 1.12 - 1.16.5 | 8 (1.8.0)" echo " 1.17 - 1.17.1 | 16" echo " 1.18 - 1.20.4 | 17" echo " 1.20.5 - 1.21.11 | 21" echo " 26.1 | 25" echo "Running Java versions newer than the requirements is possible but may result in" echo "instability, especially with the oldest versions. It is not recommended." echo "" echo "Ensure the version is available as a jdk(version)-openjdk package at" echo "https://archlinux.org/packages/?q=jdk, or it must be installed manually." echo "Make sure to check out the AUR for more versions." while true; do echo "" read -rp "$p Enter a valid version (ENTER to skip): " java case "$java" in "" ) echo "$a1 Skipping installation..." break;; *[!0-9]* ) echo "$a1 Invalid response.";; * ) echo "$a1 Installing Java $java via the OpenJDK package..." echo "$a2 Installing jdk$java-openjdk." if pacman -Sy --noconfirm "jdk$java-openjdk"; then echo "$a1 Installed OpenJDK $java." break else echo "$a1 Failed to install OpenJDK $java." fi ;; esac done # Installer. echo "" echo "Choose if you want to install a Fabric server. You should still choose 'Y' if" echo "you are re-running this script. Downloading and running the installer can still" echo "be skipped in the next step." echo "If you are installing a Fabric server with your own installer, then choose 'N'" echo "and proceed like any other manual installation." echo "The Fabric installer supports Minecraft 1.14 and later." while true; do echo "" read -rp "$p Install a Fabric server? [Y/n] " use_fabric case "$use_fabric" in [Yy]|"" ) install="fabric" break;; [Nn] ) echo "$a1 Skipping Fabric installer." echo "" echo "Choose if you want to use an existing server installer." echo "This requires an existing and accessible .jar file." while true; do echo "" read -rp "$p Use other server installer? [Y/n] " use_other case "$use_other" in [Yy]|"" ) install="manual" break;; [Nn] ) install="none" runjar="" echo "$a1 Skipping installation." break;; * ) echo "$a1 Invalid response.";; esac done break;; * ) echo "$a1 Invalid response.";; esac done # Installation. if [ "$install" != "none" ]; then if [ "$install" = "fabric" ]; then while true; do echo "" read -rp "$p Download the Fabric installer? [Y/n] " get_installer case "$get_installer" in [Yy]|"" ) get_installer="true" break;; [Nn] ) echo "$a1 Skipping..." break;; * ) echo "$a1 Invalid response.";; esac done if [ "$get_installer" = "true" ]; then echo "$a1 Getting Fabric installer..." echo "$a2 Checking latest installer version." installer_ver=$(curl -s https://maven.fabricmc.net/net/fabricmc/fabric-installer/maven-metadata.xml | grep -oPm1 '(?<=)[^<]+') echo "$a2 Got installer version: '$installer_ver'." echo "$a2 Downloading file." curl -s -o 'fabric-installer.jar' "https://maven.fabricmc.net/net/fabricmc/fabric-installer/$installer_ver/fabric-installer-$installer_ver.jar" echo "$a1 Downloaded installer." while true; do echo "" read -rp "$p Select valid Minecraft version to install (ENTER for latest): " mc case "$mc" in "" ) mc="latest" echo "$a1 Installing latest Fabric server..." echo "$a2 Running installer." if sudo -u "$user" java -jar "fabric-installer.jar" server -downloadMinecraft; then break else echo "$a1 Failed to install server." fi ;; * ) echo "$a1 Installing $mc Fabric server..." echo "$a2 Running installer." if sudo -u "$user" java -jar "fabric-installer.jar" server -mcversion "$mc" -downloadMinecraft; then break else echo "$a1 Failed to install server." fi ;; esac done echo "$a1 Installed $mc Fabric server." echo "$a2 Removing installer." rm "fabric-installer.jar" fi elif [ "$install" = "manual" ]; then echo "" echo "Enter the server installer file path." echo "Working directory is '$data_dir/minecraft' for relative file paths." echo "Using an asterisk ('*') takes the first match." while true; do echo "" read -rp "$p Enter the server installer path: " serverjar_pattern mapfile -t files < <(compgen -G "$serverjar_pattern") serverjar="${files[0]}" if [ ! -f "$serverjar" ]; then serverjar="$serverjar_pattern" echo "$a1 File not found: '$serverjar'." else echo "$a1 Found file: '$serverjar'." while true; do echo "" read -rp "$p Do you want to continue with this installer? [Y/n] " use_installer case "$use_installer" in [Yy]|"" ) echo "$a1 Selected server installer: '$serverjar'." break 2;; [Nn] ) break;; * ) echo "$a1 Invalid response.";; esac done fi done echo "" echo "Choose server installer arguments. These are NOT the runtime arguments and will" echo "have no effect after the installation is complete." echo "Forge should use 'nogui --installServer' args." echo "You can and should use '--nogui' on newer versions." while true; do echo "" read -rp "$p Enter any installer arguments (ENTER for 'nogui'): " server_args if [ ! "$server_args" ]; then server_args="nogui" fi echo "" echo "Server installer arguments: '$server_args'" while true; do echo "" read -rp "$p Do you want to continue with these arguments? [Y/n] " use_install_args case "$use_install_args" in [Yy]|"" ) use_install_args="true" echo "$a1 Selected server installer arguments: '$server_args'." break;; [Nn] ) break;; * ) echo "$a1 Invalid response.";; esac done if [ "$use_install_args" = "true" ]; then echo "$a1 Installing server..." echo "$a2 Launching executable." if sudo -u "$user" bash -c "java -jar \"$serverjar\" $server_args"; then echo "$a1 Finished installing the server." break else echo "$a1 Failed to install the server." fi fi done echo "$a2 Removing installer." rm "$serverjar" fi # File generation. while true; do echo "" echo "Normally, the server would be run once to generate a eula.txt and other config" echo "files to help set up the server. However, this is entirely optional." while true; do echo "" read -rp "$p Do you want to launch the server once? [Y/n] " launch_once case "$launch_once" in [Yy]|"" ) break;; [Nn] ) echo "$a1 Skipping..."; break 2;; * ) echo "$a1 Invalid response.";; esac done different_exec="" if [ "$install" = "fabric" ]; then runjar="fabric-server-launch.jar" echo "$a2 Checking for '$runjar'." if [ ! -f "$runjar" ]; then echo "" echo "The Fabric server launch file could not be found. Either specify an existing" echo "launch file or skip the first launch and continue until an executable is needed," echo "like if you are going to generate a run script." while true; do echo "" read -rp "$p Do you want to specify a different executable? [y/N] " different_exec case "$different_exec" in [Yy] ) different_exec="true" break;; [Nn]|"" ) echo "$a1 Skipping first launch..." break 2;; * ) echo "$a1 Invalid response.";; esac done fi fi if [ "$install" = "manual" ] || [ "$different_exec" = "true" ]; then echo "" echo "Enter the server executable file path (not the installer)." echo "Working directory is '$data_dir/minecraft' for relative file paths." echo "Using an asterisk ('*') takes the first match." while true; do echo "" read -rp "$p Enter the .jar file that runs the server (ENTER for *.jar): " runjar_pattern if [ ! "$runjar_pattern" ]; then runjar_pattern="*.jar" fi mapfile -t files < <(compgen -G "$runjar_pattern") runjar="${files[0]}" if [ ! -f "$runjar" ]; then runjar="$runjar_pattern" echo "$a2 File not found: '$runjar'." else echo "$a2 Found file: '$runjar'." while true; do echo "" read -rp "$p Do you want to continue with this executable? [Y/n] " use_exec case "$use_exec" in [Yy]|"" ) echo "$a2 Selected server executable: '$runjar'." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done fi done fi echo "" echo "Choose server executable arguments. These are NOT saved and will only be used" echo "this one time to launch the server so that config files are generated." echo "You can and should use '--nogui' on newer versions." while true; do echo "" read -rp "$p Enter any executable arguments (ENTER for 'nogui'): " run_args if [ ! "$run_args" ]; then run_args="nogui" fi echo "" echo "Server executable arguments: '$run_args'" while true; do echo "" read -rp "$p Do you want to continue with these arguments? [Y/n] " use_run_args case "$use_run_args" in [Yy]|"" ) echo "$a2 Selected server executable arguments: '$run_args'." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done done echo "$a2 Running server..." sudo -u "$user" bash -c "java -jar \"$runjar\" $run_args" echo "$a2 Finished running server." echo "" echo "If there were any errors (other than the expected EULA notice), you may want to" echo "stop here and make some changes. You can press Ctrl+C if you need to exit this" echo "script, or enter 'Y' to just re-enter the executable path and arguments." echo "Keep in mind that you do not want the server to launch fully, (which it won't" echo "as long as eula.txt is left alone,) as the only purpose of this is to generate" echo "files like a eula.txt, and ideally other configs for you to modify later." while true; do echo "" read -rp "$p Do you want to retry the installation? [y/N] " do_retry case "$do_retry" in [Yy] ) echo "$a1 Retrying..." break;; [Nn]|"" ) echo "$a1 First server launch completed." break 2;; * ) echo "$a1 Invalid response.";; esac done done # EULA. echo "$a1 Setting EULA..." echo "" echo "If no EULA file exists, one will be created with a single line: 'eula=true'." while true; do echo "" read -rp "$p Agree to the EULA? [Y/n] " eula case "$eula" in [Yy]|"" ) if [ ! -f "eula.txt" ]; then echo "$a2 Created eula.txt." echo "eula=true" > "eula.txt" else echo "$a2 Modified eula.txt to set: 'eula=true'." sed -i 's/^eula=.*/eula=true/' "eula.txt" fi echo "$a1 EULA set to true." break;; [Nn] ) echo "$a1 Skipping..." break;; * ) echo "$a1 Invalid response.";; esac done fi # Run script. echo "" echo "Do you want to create a script which launches the server with set arguments?" echo "It will be stored in the minecraft directory. Modify that file to change the" echo "arguments that are used every time the server is launched." echo "Skipping both its creation and usage will prevent systemd unit creation." while true; do echo "" read -rp "$p Create run.sh script? [Y/n] " make_script case "$make_script" in [Yy]|"" ) run_script="install" break;; [Nn] ) echo "" echo "For a run script to be \"existing\", it must be named 'run.sh' and be within the" echo "minecraft directory. Ensure this is true if it must be used." while true; do echo "" read -rp "$p Use existing run.sh script? [Y/n] " use_script case "$use_script" in [Yy]|"" ) run_script="existing" break;; [Nn] ) run_script="skip" echo "$a1 Skipping..." break;; * ) echo "$a2 Invalid response.";; esac done break;; * ) echo "$a2 Invalid response.";; esac done if [ "$run_script" = "install" ]; then echo "$a1 Creating run script with Java arguments..." echo "" echo "Choose allocated server memory in GB" echo "You should leave roughly 1-2GB available for the host. E.g. use up to 6-7GB if" echo "the system has 8GB. I recommend using 3-6 for best performance, and avoiding >8" echo "unless there are many players or mods." while true; do echo "" read -rp "$p Enter allocated memory [1-99] (ENTER to use 4GB): " mem case "$mem" in [1-9]|[1-9][0-9] ) echo "$a2 Allocating ${mem}GB." break;; "" ) echo "$a2 Allocating 4GB." mem="4" break;; * ) echo "$a2 Invalid response.";; esac done if [ ! "$runjar" ]; then echo "$a2 No .jar file has been selected to run the server." echo "" echo "Enter the server executable file path (not the installer)." echo "Working directory is '$data_dir/minecraft' for relative file paths." echo "Using an asterisk ('*') takes the first match." while true; do echo "" read -rp "$p Enter the .jar file that runs the server (ENTER for *.jar): " runjar_pattern if [ ! "$runjar_pattern" ]; then runjar_pattern="*.jar" fi mapfile -t files < <(compgen -G "$runjar_pattern") runjar="${files[0]}" if [ ! -f "$runjar" ]; then runjar="$runjar_pattern" echo "$a2 File not found: '$runjar'." else echo "$a2 Found file: '$runjar'." fi while true; do echo "" read -rp "$p Do you want to continue with this executable path? [Y/n] " use_exec case "$use_exec" in [Yy]|"" ) echo "$a2 Selected server executable: '$runjar'." break 2;; [Nn] ) break;; * ) echo "$a2 Invalid response.";; esac done done fi echo "" echo "You can and should use '--nogui' on newer versions." while true; do echo "" read -rp "$p Do you want to use '--nogui' instead of 'nogui' in the launch script? [y/N] " new_nogui case "$new_nogui" in [Yy] ) nogui_arg="--nogui" break;; [Nn]|"" ) nogui_arg="nogui" break;; * ) echo "$a1 Invalid response.";; esac done echo "$a2 Using '$nogui_arg' argument in launch script." echo "$a2 Writing script." cat > "$data_dir/minecraft/run.sh" <<- EOF cd "$data_dir/minecraft" java -Xmx${mem}G -Xms${mem}G -XX:+UseG1GC -Dsun.rmi.dgc.server.gcInterval=2147483646 -XX:+UnlockExperimentalVMOptions -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M -jar "$runjar" $nogui_arg EOF echo "$a2 Setting ownership." chown "$user:$user" "$data_dir/minecraft/run.sh" echo "$a2 Marking executable flag." chmod +x "$data_dir/minecraft/run.sh" echo "$a1 Created launch script in the minecraft directory." fi # Systemd units. sysd="" if [ "$update" != "true" ] && [ "$run_script" != "skip" ]; then echo "" echo "Choose to set up autostart by creating systemd unit files." echo "This would allow you to start/stop the server with systemctl, view logs with" echo "journalctl, and send commands to Minecraft's server console in the background." while true; do echo "" read -rp "$p Do you want to set up a systemd unit? [Y/n] " sysd case "$sysd" in [Yy]|"" ) sysd="install" break;; [Nn] ) sysd="skip" echo "$a1 Skipping..." break;; * ) echo "$a1 Invalid response.";; esac done fi if [ "$sysd" = "install" ] && [ "$run_script" != "skip" ]; then echo "$a1 Creating systemd unit files..." echo "$a2 Creating service file." cat > "/etc/systemd/system/minecraft.service" <<- EOF [Unit] Description=Dedicated Minecraft server After=network.target minecraft.socket Requires=minecraft.socket [Service] User=$user Type=exec WorkingDirectory=$data_dir/minecraft ExecStart=/usr/bin/sh -c "$data_dir/minecraft/run.sh < /run/minecraft.control" ExecStop=/usr/bin/sh -c "echo '/stop' > /run/minecraft.control" Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target EOF echo "$a2 Creating socket file." cat > "/etc/systemd/system/minecraft.socket" <<- EOF [Unit] Description=Minecraft control FIFO PartOf=minecraft.service [Socket] ListenFIFO=/run/minecraft.control SocketMode=0660 SocketUser=$user RemoveOnStop=true [Install] WantedBy=multi-user.target EOF echo "$a1 Created systemd unit files." echo "$a2 Reloading systemd daemon." systemctl daemon-reload echo "$a2 Enabling socket for FIFO creation." systemctl enable minecraft.socket &> /dev/null echo "$a2 Enabling service." systemctl enable minecraft.service &> /dev/null echo "$a1 Enabled systemd service." echo "INFO: The server is set up to start and restart automatically." echo "$a1 Creating mcc (Minecraft console) script in the data directory..." echo "$a2 Creating script." cat > "$data_dir/mcc.sh" <<- EOF #!/bin/bash if [ ! -p /run/minecraft.control ]; then echo "FIFO does not exist." exit 1 fi timestamp="\$(date +"%F %T.%N")" journalctl -o cat -fu minecraft.service --since "\$timestamp" & journal_pid=\$! printf "%s\n" "\$*" > /run/minecraft.control wait \$journal_pid EOF echo "$a2 Setting ownership." chown "$user:$user" "$data_dir/mcc.sh" echo "$a2 Marking executable flag." chmod +x "$data_dir/mcc.sh" echo "$a1 Created console script in data directory." echo "INFO: The console script passes all arguments to the server, and returns the" echo " output until stopped with Ctrl+C." add_cmd="" echo "" echo "Choose to install an mcc.sh link in /usr/bin/." echo "Creates a link to ~/mcc.sh, allowing you to run server commands from anywhere" echo "using just that command. E.g. 'mcc /gamerule keepInventory true'." echo "You will choose the command name." while true; do echo "" read -rp "$p Add mcc.sh link in /usr/bin? [Y/n] " add_cmd case "$add_cmd" in [Yy]|"" ) while true; do echo "" read -rp "$p Enter the name of the command (ENTER for 'mcc'): " mcc_cmd if [ ! "$mcc_cmd" ]; then mcc_cmd="mcc" fi if command -v "$mcc_cmd" &> /dev/null; then echo "$a2 Command '$mcc_cmd' already exists." if [ "$(command -v "$mcc_cmd")" = "/usr/bin/$mcc_cmd" ]; then echo "" echo "The command '$mcc_cmd' appears to be the file: '/usr/bin/$mcc_cmd'." if [ -L "/usr/bin/$mcc_cmd" ]; then echo "It links to '$(readlink "/usr/bin/$mcc_cmd")'." else echo "It is not a symbolic link. It was not created by this script." fi while true; do echo "" read -rp "$p Do you want to replace it? [y/N] " rm_cmd case "$rm_cmd" in [Yy] ) echo "$a2 Removing: '/usr/bin/$mcc_cmd'." rm -f "/usr/bin/$mcc_cmd" break 2;; [Nn]|"" ) break;; * ) echo "$a1 Invalid response.";; esac done else echo "" echo "It is not a file in the /usr/bin directory. It was not created by this script." echo "The path is: $(command -v "$mcc_cmd")" while true; do echo "" read -rp "$p Do you want to create a link in /usr/bin anyway? [y/N] " do_conflict case "$do_conflict" in [Yy] ) break 2;; [Nn]|"" ) break;; * ) echo "$a1 Invalid response.";; esac done fi continue 2 else echo "$a2 Command '$mcc_cmd' is available." while true; do echo "" read -rp "$p Do you want to continue with this name? [Y/n] " use_cmd case "$use_cmd" in [Yy]|"" ) echo "$a1 Selected command: '$mcc_cmd'." break 2;; [Nn] ) break;; * ) echo "$a1 Invalid response.";; esac done fi done add_cmd="true" echo "$a2 Creating symlink." ln -s "$data_dir/mcc.sh" "/usr/bin/$mcc_cmd" echo "$a1 Created '$mcc_cmd' link." break;; [Nn] ) add_cmd="false" echo "$a1 Skipping..." break;; * ) echo "$a1 Invalid response.";; esac done fi if [ "$install" != "none" ]; then echo "" echo "Installation complete, the server is not running." echo "NOTICE: Please configure server.properties before starting the server." if [ "$install" = "fabric" ]; then echo "NOTICE: It's recommended to download performance mods such as Lithium," echo " FerriteCore, ModernFix, and possibly others." fi if [ "$run_script" != "skip" ]; then if [ "$sysd" = "install" ]; then echo "" echo "Systemd is in use, the server will automatically start after boot." echo "NOTICE: The server can be stopped by using either systemd, the /stop command," echo " or shutting down the host. Each method performs a clean shutdown, so the" echo " server won't lose data. You may notice a delay shutting down the host as" echo " the Minecraft server stops." echo "" echo "Many commands are available with systemd, some important ones are:" echo "Executing commands in the Minecraft server console using a script:" echo " $ ./mcc.sh " if [ "$add_cmd" = "true" ]; then echo "Or by using the '$mcc_cmd' command:" echo " # $mcc_cmd " fi echo "View the server logs with journalctl: (Shift+G jumps to the end)" echo " $ journalctl -u minecraft" echo "Or follow them as they update with:" echo " $ journalctl -fu minecraft" echo "And easily perform a clean start/stop of the server with:" echo " # systemctl {start,stop,restart} minecraft" else echo "" echo "Start the server with the run script, or set up a user crontab for autostart." echo " $ ./run.sh" fi fi fi echo "$a1 Finished."