LaoArchAngel

Infinity Java 7 Startup

Jul 16th, 2015
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.23 KB | None | 0 0
  1. #!/usr/bin/bash
  2.  
  3. # Source from http://minecraft.gamepedia.com/Tutorials/Server_startup_script
  4. # Made changes to suit my needs, now serves as a proper croncheck script
  5. # There will be bugs.  Fix them.  Send me a note about it if you like and
  6. # I'll include them.  I can be reached on reddit as /u/jbranscum
  7. # This is open source.  Don't be a jerk.  Give credit where it is due if you
  8. # decide to modify this script as I have done
  9. #
  10. # Replace HOMEDIR, MCUSERNAME, MCSERVERDIR as needed.
  11. # Cron entry should look like so:
  12. # */5 * * * * /home/HOMEDIR/scripts/mcinit croncheck
  13. # 30 04 * * * /home/HOMEDIR/scripts/mcinit restart
  14. #
  15. # You need screen, uuencode and mailx for this script in addition to standard bash tools (pgrep, etc)
  16. # I cannot provide support on this.  Use at your own risk.  You have been warned.
  17. #
  18. # If you're unsure of yourself, see the following: http://youtu.be/nuHfVn_cfHU
  19. #
  20. # Also, these are Java 8 settings to use 4/8 gig ram.  If you have Java 7, make changes!
  21.  
  22. #Settings
  23. PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin
  24. SERVICE='KCauldron.jar'
  25. OPTIONS='nogui'
  26. USERNAME='root'
  27. MCPATH='/usr/local/games/minecraft/infinity'
  28. MCNOSTART=$MCPATH/.nostart
  29. MCCRASH=$MCPATH/crash-reports
  30. MCLOG=$MCPATH/logs/fml-server-latest.log
  31. MAXHEAP=4096
  32. MINHEAP=2048
  33. PERM=256
  34. HISTORY=1024
  35. CPU_COUNT=8
  36. INVOCATION="java -server \
  37. -Xms${MINHEAP}m \
  38. -Xmx${MAXHEAP}m \
  39. -XX:PermSize=${PERM}m \
  40. -XX:d64 \
  41. -XX:+UseParNewGC \
  42. -XX:+CMSIncrementalPacing \
  43. -XX:+CMSClassUnloadingEnabled \
  44. -XX:ParallelGCThreads=2 \
  45. -XX:MinHeapFreeRatio=5 \
  46. -XX:MaxHeapFreeRatio=10 \
  47. -Djava.net.preferIPv4Stack=true \
  48. -jar $SERVICE $OPTIONS"
  49.  
  50. ME=`whoami`
  51. as_user() {
  52.   if [ "$ME" = "$USERNAME" ] ; then
  53.     bash -c "$1"
  54.   else
  55.     su - "$USERNAME" -c "$1"
  56.   fi
  57. }
  58.  
  59. mc_start() {
  60.   if pgrep -u $USERNAME -f $SERVICE > /dev/null
  61.   then
  62.     echo "$SERVICE is already running!"
  63.   else
  64.     echo "Starting $SERVICE..."
  65.     cd $MCPATH
  66.     as_user "cd $MCPATH && screen -h $HISTORY -dmS minecraft $INVOCATION"
  67.     sleep 7
  68.     if pgrep -u $USERNAME -f $SERVICE > /dev/null
  69.     then
  70.       echo "$SERVICE is now running."
  71.     else
  72.       echo "Error! Could not start $SERVICE!"
  73.     fi
  74.   fi
  75. }
  76.  
  77. mc_disableAuto() {
  78.     echo "Disabling autostart"
  79.     touch $MCNOSTART
  80. }
  81.  
  82. mc_enableAuto() {
  83.   if [[ -a $MCNOSTART ]] ; then
  84.     echo "Enabling autostart"
  85.     rm $MCNOSTART
  86.   fi
  87. }
  88.  
  89. mc_stop() {
  90.   if pgrep -u $USERNAME -f $SERVICE > /dev/null
  91.   then
  92.     echo "Stopping $SERVICE"
  93.     as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SHUTTING DOWN IN 60 SECONDS.\"\015'"
  94.     sleep 30
  95.     as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SHUTTING DOWN IN 30 SECONDS.\"\015'"
  96.     sleep 15
  97.     as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SHUTTING DOWN IN 15 SECONDS.\"\015'"
  98.     sleep 15
  99.     as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
  100.     sleep 15
  101.   else
  102.     echo "$SERVICE was not running."
  103.   fi
  104.   if pgrep -u $USERNAME -f $SERVICE > /dev/null
  105.   then
  106.     echo "Error! $SERVICE could not be stopped."
  107.   else
  108.     echo "$SERVICE is stopped."
  109.   fi
  110. }
  111.  
  112. mc_restartannounce() {
  113.   if pgrep -u $USERNAME -f $SERVICE > /dev/null
  114.   then
  115.     echo "Broadcasting Restart"
  116.     as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SCHEDULED RESTART IMMINENT, RESISTANCE IS FUTILE\"\015'"
  117.     sleep 20
  118.   else
  119.     echo "$SERVICE was not running."
  120.     exit 1
  121.   fi
  122. }
  123.  
  124. mc_stopnow() {
  125.   if pgrep -u $USERNAME -f $SERVICE > /dev/null
  126.   then
  127.     echo "Stopping $SERVICE"
  128.     as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SHUTTING DOWN IN 5 SECONDS.\"\015'"
  129.     sleep 5
  130.     as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
  131.     sleep 15
  132.   else
  133.     echo "$SERVICE was not running."
  134.   fi
  135.   if pgrep -u $USERNAME -f $SERVICE > /dev/null
  136.   then
  137.     echo "Error! $SERVICE could not be stopped."
  138.   else
  139.     echo "$SERVICE is stopped."
  140.   fi
  141. }
  142.  
  143. mc_logcheck() {
  144.   CRASH=$(find $MCCRASH -maxdepth 1 -name '*.log' -print -quit -cmin -60)
  145.   MCLOGLAST=$(stat -x $MCLOG)
  146.   MCLOGDATA=$(tail -n 150 $MCLOG)
  147.   if [[ -n $CRASH ]] ; then
  148.     echo "We have a crash log: $CRASH"
  149.     echo "Sending email to local account"
  150.     ( $MCLOGLAST ; tail -n 150 $MCLOG ; uuencode $CRASH $CRASH ) | mailx -s "Minecraft Server Crash" $USERNAME
  151.   else
  152.     if [[ ! -a $MCNOSTART ]] ; then
  153.       echo "Server not running, Autostart enabled"
  154.       echo "Last log entry from FML:"
  155.       stat -f "%m%t%Sm %N" $MCLOG | cut -f2
  156.       echo "Restarting Minecraft Server"
  157.       sleep 10
  158.       mc_start
  159.     fi
  160.   fi
  161. }
  162.  
  163. #Start-Stop here
  164. case "$1" in
  165.   start)
  166.     mc_start
  167.     mc_enableAuto
  168.     ;;
  169.   stop)
  170.     mc_disableAuto
  171.     mc_stop
  172.     ;;
  173.   stopnow)
  174.     mc_disableAuto
  175.     mc_stopnow
  176.     ;;
  177.   restart)
  178.     mc_disableAuto
  179.     mc_restartannounce
  180.     mc_stopnow
  181.     sleep 20
  182.     mc_start
  183.     mc_enableAuto
  184.     ;;
  185.   status)
  186.     if pgrep -u $USERNAME -f $SERVICE > /dev/null
  187.     then
  188.       echo "$SERVICE is running."
  189.     else
  190.       echo "$SERVICE is not running."
  191.     fi
  192.     ;;
  193.   croncheck)
  194.     if ! pgrep -u $USERNAME -f $SERVICE > /dev/null
  195.     then
  196.       mc_logcheck
  197.     fi
  198.     ;;
  199.   *)
  200.   echo "Usage: $0 {start|stop|stopnow|restart|status|croncheck}"
  201.   exit 1
  202.   ;;
  203. esac
  204.  
  205. exit 0
Advertisement
Add Comment
Please, Sign In to add comment