Advertisement
Guest User

auto-restart.sh (bugged)

a guest
Oct 12th, 2018
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.70 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #    ###############################
  4. #    ##  ~About / How to use this~ #
  5. #    ###############################
  6. #
  7. #    auto-restart.sh
  8. #
  9. #    This script is intended to be launched by a cronjob task every minute
  10. #    to check for any offline game servers. The cronjob would look like this:
  11. #        * * * * * /path/to/auto-restart.sh
  12. #
  13. #    (this script assumes every game server has it's own startup script and a unique screen name)
  14. #
  15. #    You can add as many servers as you like. HOWEVER, keep the name the same
  16. #    BEFORE the equal sign. e.g. server1, server2, server3. & start_svr1, start_svr2,
  17. #    start_svr3.
  18. #
  19. #    Screen names should be unique enough that the full name of one server isn't found in another.
  20. #    e.g. L4D2_svr1 & L4D2_svr2 would be OK, but this would be problematic: L4D2_svr & L4D2_svr2
  21. #
  22. #    EXAMPLE customization:
  23. #    NUM_SERVERS=3
  24. #    
  25. #    server1=L4D2_svr1  // screen names, assigned in startup script ("screen -mdS L4D2_svr1")
  26. #    server2=L4D2_svr2
  27. #    server3=CSGO_svr
  28. #    
  29. #    start_svr1=/path/to/l4d2svr1.sh    // path to each server's start (bootup) script
  30. #    start_svr2=/path/to/l4d2svr2.sh
  31. #    start_svr3=/path/to/CSGOsvr.sh
  32.  
  33. # The three checks before launching a game server are because the "alive" variable can sometimes incorrectly return a value of 1 (no server detected)
  34. #  https://i.imgur.com/UDsFTil.png      so it would relaunch the game server when it's already running...
  35.  
  36. #############################
  37. # Customization starts HERE #
  38. #############################
  39.  
  40. # How many game servers on your machine?
  41. NUM_SERVERS=2
  42.  
  43. ###########################################
  44. # Edit your game server screen names HERE #
  45. ###########################################
  46. #note: DON'T edit before the equal sign
  47.  
  48. server1=left4dead2_server
  49. server2=CSGO_server
  50.  
  51. #server3=  #screen name for server 3
  52. #server4=  #screen name for server 4
  53. #etc..
  54.  
  55. ####################################
  56. # Edit path to launch scripts HERE #
  57. ####################################
  58. #note: DON'T edit before the equal sign
  59.  
  60. start_svr1=/root/start.sh
  61. start_svr2=/root/startcsgo.sh
  62.  
  63. #start_svr3= #/path/to/start.sh for server 3
  64. #start_svr4= #/path/to/start.sh for server 4
  65. #etc..
  66.  
  67. ###############################
  68. # Customization ends HERE     #
  69. ###############################
  70.  
  71. # Don't edit this part:
  72.  
  73. scriptcheck=`pgrep -fcl auto-restart.sh`
  74. if [ $scriptcheck -gt 1 ]; then
  75.     echo "auto-restart.sh script already in use. Terminating.."
  76.     #cron job debugging..
  77.     echo "[DEBUG] scriptcheck returning greater than 1" > /tmp/debug.output
  78.     echo "[DEBUG] Value of scriptcheck: ${scriptcheck}" >> /tmp/debug.output
  79.     echo "[DEBUG] contents:" >> /tmp/debug.output
  80.     pgrep -fl auto-restart.sh >> /tmp/debug.output
  81.     exit
  82. fi
  83.  
  84. i=1;
  85. while [[ i -le ${NUM_SERVERS} ]] ;
  86. do
  87.  
  88. var="server$i"
  89. var2="start_svr$i"
  90.  
  91. echo ""
  92. echo "--------------------------------"
  93. echo ""
  94. echo "Initiating check on server #${i}: ${!var}"
  95. echo ""
  96. sleep 1s
  97.  
  98. alive=`ps ux | grep "${!var}" | wc -l | awk '{ print $1 }'`
  99.  
  100. if [ $alive -lt 2 ]; then
  101.     echo "Check #1: Game server offline."
  102.     sleep 3s
  103.    
  104.     alive2=`ps ux | grep "${!var}" | wc -l | awk '{ print $1 }'`
  105.     if [ $alive2 -lt 2 ]; then
  106.             echo "Check #2: Game server still offline."
  107.             sleep 3s
  108.            
  109.             alive3=`ps ux | grep "${!var}" | wc -l | awk '{ print $1 }'`
  110.             if [ $alive3 -lt 2 ]; then
  111.                 echo "Check #3: Starting game server..."
  112.                 sleep 3s
  113.                 ${!var2}
  114.             else
  115.                 echo "Third Check: game server already active. Skipping.."
  116.                 sleep 3s
  117.             fi
  118.     else
  119.         echo "Second Check: game server already active. Skipping.."
  120.         sleep 3s
  121.     fi
  122. else
  123.     echo "First Check: game server already active. Skipping.."
  124.     sleep 3s
  125. fi
  126.  
  127. i=$((i+1));
  128.  
  129. echo ""
  130. done
  131.  
  132. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement