Linux Scripts - GeheimagentNr1/AutoRestart GitHub Wiki

Everything, that is in angle brackets (<>) in the examples, must be replaced, by what is described in them.
All scripts on this page are shell scripts and must be saved with the ending ".sh".

Before you use the scripts:

  • Save them with Linux line endings (LF). Scripts edited on Windows have Windows line endings (CRLF) and fail with errors like /bin/bash^M: bad interpreter or $'\r': command not found. You can convert them with sed -i 's/\r$//' *.sh or dos2unix *.sh.
  • Make them executable with chmod +x *.sh.
  • Put paths with spaces in double quotes, i.e. cd "/opt/minecraft/servers/Minecraft Server".

If you have problems, check the Troubleshooting page.

Java command

The Java command to start the server depends on the mod loader and the Minecraft version:

  • Forge 1.16.5 or older: java -Xmx<Amount of RAM in gigabytes you want to allocate to Minecraft>G -jar forge-<Minecraft version>-<Forge version>.jar
  • Forge 1.17.1 or newer: java -Xmx<Amount of RAM in gigabytes you want to allocate to Minecraft>G @libraries/net/minecraftforge/forge/<Minecraft version>-<Forge version>/unix_args.txt
  • NeoForge (1.20.2 or newer): java -Xmx<Amount of RAM in gigabytes you want to allocate to Minecraft>G @libraries/net/neoforged/neoforge/<NeoForge version>/unix_args.txt

In the examples below, replace <Java command> by the matching command.

Restart Command Script

This script can be used to run the server once, i.e. if you configured Auto Restart to not use an external restart script.
You configure "restart_command" as the path of this script, i.e. "<Path of your Minecraft server>/start.sh".
"restart_command" must only contain the path of the script, without any other commands or arguments. Values like "bash start.sh", "./start.sh nogui" or "cd ... && ./start.sh" don't work, because the whole value is used as the name of the program to start. Put everything else into the script.
The script must start the server in the background (i.e. in a screen) and must end directly. It must not wait for input (no read), because it is started by the stopping server and has no console.

#!/bin/bash
cd "<Path of your Minecraft server>" || exit 1
screen -d -m -S <Name of your Screen, i.e. your folders name without spaces> <Java command>

External Restart Script

These scripts are for the use, if you have configured, that you are using an external restart script.
The first script is named "restart_start.sh". It is used to start the "restart_loop.sh" script in a screen.

#!/bin/bash
cd "<Path of your Minecraft server>" || exit 1
screen -d -m -S <Name of your Screen, i.e. your folders name without spaces> ./restart_loop.sh

The "restart_loop.sh" script has the logic in it, to restart or stop the server. It will restart the server always, except if the "./auto_restart/restart" file contains "0".
The Auto Restart Mod writes "-1", "0" or "1" to the file. When the script reads the file,

  • "-1" means that the server crashed.
  • "0" means the server was shutdown with the "/stop" command and should be stopped.
  • "1" means that the server was stopped with the "/restart" command or an auto restart and should be restarted.

After the server was stopped for a restart, the script waits 5 seconds until it restarts the Minecraft server.

#!/bin/bash
while true
do
    <Java command>
    read -r should_restart < ./auto_restart/restart
    if [ "$should_restart" = "0" ]
    then
        break
    fi
    echo 'If you want to stop the server completely, press Ctrl+C before the countdown is at 1!'
    echo "Rebooting in:"
    for i in 5 4 3 2 1
    do
        echo "$i..."
        sleep 1
    done
    echo 'Server restarts!'
done