Advertisement
Guest User

Untitled

a guest
Sep 29th, 2015
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. #!/bin/bash
  2. # /etc/init.d/minecraft
  3. # version 0.4.1 2015-05-07 (YYYY-MM-DD)
  4. #
  5. ### BEGIN INIT INFO
  6. # Provides: minecraft
  7. # Required-Start: $local_fs $remote_fs screen-cleanup
  8. # Required-Stop: $local_fs $remote_fs
  9. # Should-Start: $network
  10. # Should-Stop: $network
  11. # Default-Start: 2 3 4 5
  12. # Default-Stop: 0 1 6
  13. # Short-Description: Minecraft server
  14. # Description: Starts the minecraft server
  15. ### END INIT INFO
  16.  
  17. ### SETTINGS
  18. #
  19. # Server Name
  20. SERVERNAME='craftbukkit-1.8.8'
  21. # Linux Username
  22. USERNAME='minecraft'
  23. # Server Root Folder
  24. MCPATH="/home/Minecraft"
  25. # Server Jar File
  26. SERVERJAR="$SERVERNAME.jar"
  27. # Max RAM to Allocate
  28. RAM='2G'
  29. # Server Java Command
  30. INVOCATION="java -Xmx${RAM} -Xms1G -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=128M -jar $SERVERJAR nogui"
  31. # Minecraft Worlds
  32. WORLDS=("world" "world_nether" "world_the_end")
  33. # Backup Folder
  34. BACKUPPATH="/home/$USERNAME/Backups/$SERVERNAME"
  35. # Vote Message
  36. VOTECMD="say Have you voted today? Use command /vote"
  37.  
  38. ME=`whoami`
  39. as_user() {
  40. echo "$1"
  41. if [ $ME == $USERNAME ] ; then
  42. bash -c "$1"
  43. else
  44. su - $USERNAME -c "$1"
  45. fi
  46. }
  47.  
  48. mc_start() {
  49. if pgrep -u $USERNAME -f $SERVERJAR > /dev/null
  50. then
  51. echo "$SERVERJAR is already running."
  52. else
  53. echo "Starting $SERVERJAR..."
  54. cd $MCPATH
  55. as_user "cd $MCPATH && screen -dmS $SERVERNAME $INVOCATION"
  56. # Wait 5 seconds to start up
  57. sleep 5
  58. if pgrep -u $USERNAME -f $SERVERJAR > /dev/null
  59. then
  60. # Print output
  61. tail -n $[`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`] "$MCPATH/logs/latest.log"
  62. else
  63. echo "Could not start $SERVERJAR."
  64. fi
  65. fi
  66. }
  67.  
  68. mc_stop() {
  69. if pgrep -u $USERNAME -f $SERVERJAR > /dev/null
  70. then
  71. pre_log_len=`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`
  72. echo "$SERVERJAR is running. Stopping server..."
  73. as_user "screen -p 0 -S $SERVERNAME -X eval 'stuff \"stop\"\015'"
  74. # Wait 10 seconds to finish stopping
  75. sleep 10
  76. # Print output
  77. tail -n $[`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`-$pre_log_len] "$MCPATH/logs/latest.log"
  78. else
  79. echo "$SERVERJAR is not running..."
  80. fi
  81. }
  82.  
  83. mc_command() {
  84. command="$1";
  85. if [ "$command" == "" ]
  86. then
  87. echo "Usage: server.sh [ start | stop | restart | status | save | backup | \"command\" ]"
  88. return
  89. fi
  90. if pgrep -u $USERNAME -f $SERVERJAR > /dev/null
  91. then
  92. pre_log_len=`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`
  93. echo "$SERVERJAR is running. Executing $command..."
  94. as_user "screen -p 0 -S $SERVERNAME -X eval 'stuff \"$command\"\015'"
  95. # Wait 1 second for the command to run and print to the log file
  96. sleep 1
  97. # Print output
  98. tail -n $[`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`-$pre_log_len] "$MCPATH/logs/latest.log"
  99. else
  100. echo "$SERVERJAR is not running..."
  101. fi
  102. }
  103.  
  104. mc_saveall() {
  105. if pgrep -u $USERNAME -f $SERVERJAR > /dev/null
  106. then
  107. echo "$SERVERJAR is running. Saving the world..."
  108. # Save the World
  109. as_user "screen -p 0 -S $SERVERNAME -X eval 'stuff \"save-all\"\015'"
  110. # Say World saved.
  111. as_user "screen -p 0 -S $SERVERNAME -X eval 'stuff \"say World saved.\"\015'"
  112. # Save WorldGuard Regions
  113. as_user "screen -p 0 -S $SERVERNAME -X eval 'stuff \"region save\"\015'"
  114. sync
  115. # Wait 10 seconds to finish saving
  116. sleep 10
  117. echo "World saved."
  118. else
  119. echo "$SERVERJAR was not running. Noting to save."
  120. fi
  121. }
  122.  
  123. mc_backup() {
  124. NOW=`date "+%Y-%m-%d_%Hh%M"`
  125.  
  126. # Backup Server World
  127. for world in ${!WORLDS[*]}
  128. do
  129. echo "Backing up world ${WORLDS[$world]}..."
  130. as_user "cd $MCPATH && zip -r $BACKUPPATH/${WORLDS[$world]}_${NOW}.zip ${WORLDS[$world]}"
  131. echo "${WORLDS[$world]} has been archived."
  132. done
  133.  
  134. # Backup Server Plugins
  135. echo "Backing up server plugins..."
  136. as_user "cd $MCPATH && zip -r --exclude=plugins/dynmap/web/tiles/* $BACKUPPATH/Plugins_${NOW}.zip plugins"
  137. echo "The plugins have been archived."
  138.  
  139. # Backup Server Data (Bans, White-list, etc...)
  140. echo "Backing up server data..."
  141. as_user "cd $MCPATH && zip $BACKUPPATH/Server_${NOW}.zip *"
  142. echo "The server data has been archived."
  143. echo "Server backup complete."
  144. }
  145.  
  146. mc_status() {
  147. if pgrep -u $USERNAME -f $SERVERJAR > /dev/null
  148. then
  149. echo "$SERVERJAR is running."
  150. else
  151. echo "$SERVERJAR is not running."
  152. fi
  153. }
  154.  
  155. #Start-Stop here
  156. case "$1" in
  157. stop)
  158. mc_stop
  159. ;;
  160. restart)
  161. mc_stop
  162. mc_start
  163. ;;
  164. spigot-restart)
  165. # Wait 30 seconds for the server to fully stop then start again
  166. sleep 30
  167. mc_start
  168. ;;
  169. save)
  170. mc_saveall
  171. ;;
  172. backup)
  173. mc_saveall
  174. mc_command "save-off"
  175. mc_backup
  176. mc_command "save-on"
  177. ;;
  178. status)
  179. mc_status
  180. ;;
  181. votemsg)
  182. mc_command $VOTECMD
  183. ;;
  184. start)
  185. mc_start
  186. ;;
  187. *)
  188. echo "command received"
  189. mc_command "$*"
  190. ;;
  191. esac
  192.  
  193. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement