Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # ###############################
- # ## ~About / How to use this~ #
- # ###############################
- #
- # auto-restart.sh
- #
- # This script is intended to be launched by a cronjob task every minute
- # to check for any offline game servers. The cronjob would look like this:
- # * * * * * /path/to/auto-restart.sh
- #
- # (this script assumes every game server has it's own startup script and a unique screen name)
- #
- # You can add as many servers as you like. HOWEVER, keep the name the same
- # BEFORE the equal sign. e.g. server1, server2, server3. & start_svr1, start_svr2,
- # start_svr3.
- #
- # Screen names should be unique enough that the full name of one server isn't found in another.
- # e.g. L4D2_svr1 & L4D2_svr2 would be OK, but this would be problematic: L4D2_svr & L4D2_svr2
- #
- # EXAMPLE customization:
- # NUM_SERVERS=3
- #
- # server1=L4D2_svr1 // screen names, assigned in startup script ("screen -mdS L4D2_svr1")
- # server2=L4D2_svr2
- # server3=CSGO_svr
- #
- # start_svr1=/path/to/l4d2svr1.sh // path to each server's start (bootup) script
- # start_svr2=/path/to/l4d2svr2.sh
- # start_svr3=/path/to/CSGOsvr.sh
- # The three checks before launching a game server are because the "alive" variable can sometimes incorrectly return a value of 1 (no server detected)
- # https://i.imgur.com/UDsFTil.png so it would relaunch the game server when it's already running...
- #############################
- # Customization starts HERE #
- #############################
- # How many game servers on your machine?
- NUM_SERVERS=2
- ###########################################
- # Edit your game server screen names HERE #
- ###########################################
- #note: DON'T edit before the equal sign
- server1=left4dead2_server
- server2=CSGO_server
- #server3= #screen name for server 3
- #server4= #screen name for server 4
- #etc..
- ####################################
- # Edit path to launch scripts HERE #
- ####################################
- #note: DON'T edit before the equal sign
- start_svr1=/root/start.sh
- start_svr2=/root/startcsgo.sh
- #start_svr3= #/path/to/start.sh for server 3
- #start_svr4= #/path/to/start.sh for server 4
- #etc..
- ###############################
- # Customization ends HERE #
- ###############################
- # Don't edit this part:
- scriptcheck=`pgrep -fcl auto-restart.sh`
- if [ $scriptcheck -gt 1 ]; then
- echo "auto-restart.sh script already in use. Terminating.."
- #cron job debugging..
- echo "[DEBUG] scriptcheck returning greater than 1" > /tmp/debug.output
- echo "[DEBUG] Value of scriptcheck: ${scriptcheck}" >> /tmp/debug.output
- echo "[DEBUG] contents:" >> /tmp/debug.output
- pgrep -fl auto-restart.sh >> /tmp/debug.output
- exit
- fi
- i=1;
- while [[ i -le ${NUM_SERVERS} ]] ;
- do
- var="server$i"
- var2="start_svr$i"
- echo ""
- echo "--------------------------------"
- echo ""
- echo "Initiating check on server #${i}: ${!var}"
- echo ""
- sleep 1s
- alive=`ps ux | grep "${!var}" | wc -l | awk '{ print $1 }'`
- if [ $alive -lt 2 ]; then
- echo "Check #1: Game server offline."
- sleep 3s
- alive2=`ps ux | grep "${!var}" | wc -l | awk '{ print $1 }'`
- if [ $alive2 -lt 2 ]; then
- echo "Check #2: Game server still offline."
- sleep 3s
- alive3=`ps ux | grep "${!var}" | wc -l | awk '{ print $1 }'`
- if [ $alive3 -lt 2 ]; then
- echo "Check #3: Starting game server..."
- sleep 3s
- ${!var2}
- else
- echo "Third Check: game server already active. Skipping.."
- sleep 3s
- fi
- else
- echo "Second Check: game server already active. Skipping.."
- sleep 3s
- fi
- else
- echo "First Check: game server already active. Skipping.."
- sleep 3s
- fi
- i=$((i+1));
- echo ""
- done
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement