diff --git a/Minecraft/Modded/StartServer.sh b/Minecraft/Modded/StartServer.sh new file mode 100644 index 0000000..22a7d0b --- /dev/null +++ b/Minecraft/Modded/StartServer.sh @@ -0,0 +1,62 @@ +#!/bin/bash +#Claytonia.net + +# Game and server details +game="moddedmc" +server="BetterMC" + +# Discord webhook details +DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/108116989/r6m9CEumGdsrU7D6MxIS-D" +DISCORD_USERNAME="ModdedMC" +DISCORD_AVATAR_URL="https://i.imgur.com/oBycxyt.png" + +# Monitoring details +# What to look for in the log file that tells the script the server is done saving and ready to restart. +MONITOR_STRING="Stopped IO worker" +# Where is the log file? +LOG_FILE="/home/$game/$server/logs/latest.log" + +# Functions +start_server() { + # Sleep to give time for all start.sh discord msgs to be sent + sleep 3 + + # Kill any previous screen sessions + screen -X -S "$game" quit > /dev/null 2>&1 + + # Start the screen and run the server + cd "/home/$game/$server/" + screen -dmS "$game" ./start.sh && echo "Restarted at: $(date)" >>script.log +} + +# Ensure the working directory exists +cd "/home/$game/$server/" || exit + +# Notify Discord about server restart +discord_message="Saving world and restarting the server." +curl -H "Content-Type: application/json" -X POST -d '{"username": "'"$DISCORD_USERNAME"'", "content": "'"$discord_message"'", "avatar_url": "'"$DISCORD_AVATAR_URL"'"}' "$DISCORD_WEBHOOK_URL" + +# Run the screen command to stop the server +output=$(screen -p 0 -S "$game" -X eval "stuff stop\015" 2>&1) + +# Check if the output contains the "No screen session found" message +if [[ $output == *"No screen session found."* ]]; then + echo "Starting Server" + start_server + exit +else + echo "Saving World, Please wait." + echo "This takes some time!" + + # Tail the log file and monitor for the specified string + #waits for the server to fully save before starting the server instead of force quitting the screen + tail -n 0 -F "$LOG_FILE" | while read -r line; do + if [[ $line == *"$MONITOR_STRING"* ]]; then + echo "Starting Server" + start_server + exit + fi + done +fi + +exit \ No newline at end of file