Guest User

Untitled

a guest
Apr 30th, 2012
1,964
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # /etc/init.d/minecraft
  3. # version 0.5 2011-09-24 (YYYY-MM-DD)
  4.  
  5. ### BEGIN INIT INFO
  6. # Provides: minecraft
  7. # Required-Start: $local_fs $remote_fs
  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. SERVICE='craftbukkit.jar'
  19. USERNAME="sup4y0sh1"
  20. MCSTORE='/home/sup4y0sh1/minecraft'
  21. MCPATH='/home/sup4y0sh1/minecraft_ramdisk'
  22. CPU_COUNT=1
  23. INVOCATION="java -Xmx14500M -Xms4048M -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:+AggressiveOpts -jar $SERVICE nogui"
  24. BACKUPPATH='/home/sup4y0sh1/minecraft_backups/'
  25.  
  26.  
  27. ME=`whoami`
  28. as_user() {
  29. if [ "$ME" == "$USERNAME" ] ; then
  30. bash -c "$1"
  31. else
  32. su - $USERNAME -c "$1"
  33. fi
  34. }
  35.  
  36. mc_start() {
  37. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  38. then
  39. echo "Tried to start but $SERVICE was already running!"
  40. else
  41. echo "$SERVICE was not running... starting."
  42. cd $MCPATH
  43. if [ ! -f "$MCPATH/minecraft_server.jar" ]
  44. then
  45. echo "Ram drive empty... prepping."
  46. as_user "cp -R $MCSTORE/* $MCPATH/"
  47. fi
  48. as_user "cd $MCPATH && screen -dmS minecraft $INVOCATION"
  49. sleep 7
  50. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  51. then
  52. echo "$SERVICE is now running."
  53. else
  54. echo "Could not start $SERVICE."
  55. fi
  56. fi
  57. }
  58.  
  59. mc_saveoff() {
  60. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  61. then
  62. echo "$SERVICE is running... suspending saves"
  63. as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER BACKUP STARTING. Server going readonly...\"\015'"
  64. as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-off\"\015'"
  65. as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
  66. sync
  67. sleep 10
  68. else
  69. echo "$SERVICE was not running. Not suspending saves."
  70. fi
  71. }
  72.  
  73. mc_saveon() {
  74. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  75. then
  76. echo "$SERVICE is running... re-enabling saves"
  77. as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'"
  78. as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER BACKUP ENDED. Server going read-write...\"\015'"
  79. else
  80. echo "$SERVICE was not running. Not resuming saves."
  81. fi
  82. }
  83.  
  84. mc_stop() {
  85. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  86. then
  87. echo "$SERVICE is running... stopping."
  88. as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER SHUTTING DOWN IN 5 SECONDS. Saving map...\"\015'"
  89. as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
  90. sleep 5
  91. as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
  92. sleep 5
  93. else
  94. echo "$SERVICE was not running."
  95. fi
  96. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  97. then
  98. echo "$SERVICE could not be shut down... still running."
  99. else
  100. echo "$SERVICE is shut down."
  101. fi
  102. }
  103.  
  104.  
  105. mc_update() {
  106. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  107. then
  108. echo "$SERVICE is running! Will not start update."
  109. else
  110. MC_SERVER_URL=http://minecraft.net/`wget -q -O - http://www.minecraft.net/download.jsp | grep minecraft_server.jar\</a\> | cut -d \" -f 2`
  111. as_user "cd $MCPATH && wget -q -O $MCPATH/minecraft_server.jar.update $MC_SERVER_URL"
  112. if [ -f $MCPATH/minecraft_server.jar.update ]
  113. then
  114. if `diff $MCPATH/minecraft_server.jar $MCPATH/minecraft_server.jar.update >/dev/null`
  115. then
  116. echo "You are already running the latest version of $SERVICE."
  117. else
  118. as_user "mv $MCPATH/minecraft_server.jar.update $MCPATH/minecraft_server.jar"
  119. echo "Minecraft successfully updated."
  120. fi
  121. else
  122. echo "Minecraft update could not be downloaded."
  123. fi
  124. fi
  125. }
  126.  
  127. mc_backup() {
  128. echo "Backing up minecraft files"
  129. if [ -f $BACKUPPATH/MCBKUP_`date "+%Y.%m.%d"`.tar.gz ]
  130. then
  131. for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  132. do
  133. if [ -f $BACKUPPATH/MCBKUP_`date "+%Y.%m.%d"`-$i.tar.gz ]
  134. then
  135. continue
  136. else
  137. as_user "cd $MCSTORE && tar zcf $BACKUPPATH/MCBKUP_`date "+%Y.%m.%d"`-$i.tar.gz ."
  138. break
  139. fi
  140. done
  141. else
  142. as_user "cd $MCSTORE && tar zcf $BACKUPPATH/MCBKUP_`date "+%Y.%m.%d"`.tar.gz ."
  143. fi
  144. echo "Backup complete"
  145. }
  146.  
  147. mc_disksaverun() {
  148.  
  149. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  150. then
  151. echo "Saving ramdrive to disk."
  152. if [ ! -f $MCPATH/$SERVICE ]
  153. then
  154. echo "Error.. Minecraft not in ram"
  155. else
  156. if [ -d $MCSTORE/world2 ]
  157. then
  158. as_user "rm -r $MCSTORE/world2"
  159. fi
  160. if [ -d $MCSTORE/world ]
  161. then
  162. as_user "mv $MCSTORE/world $MCSTORE/world2"
  163. fi
  164.  
  165. as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-off\"\015'"
  166. as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
  167. as_user "cp -R $MCPATH/* $MCSTORE/"
  168. as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'"
  169.  
  170. if [ -d $MCSTORE/world2 ]
  171. then
  172. as_user "rm -r $MCSTORE/world2"
  173. fi
  174. fi
  175. else
  176. echo "Service is not running"
  177. fi
  178.  
  179. }
  180.  
  181. mc_disksavehalt() {
  182. echo "Saving ramdrive to disk."
  183. if [ ! -f $MCPATH/craftbukkit.jar ]
  184. then
  185. echo "Error.. Minecraft not in ram"
  186. else
  187. if [ -d $MCSTORE/world2 ]
  188. then
  189. as_user "rm -r $MCSTORE/world2"
  190. fi
  191. if [ -d $MCSTORE/world ]
  192. then
  193. as_user "mv $MCSTORE/world $MCSTORE/world2"
  194. fi
  195.  
  196. echo "Saving, screen session closed"
  197. as_user "cp -R $MCPATH/* $MCSTORE/"
  198.  
  199. if [ -d $MCSTORE/world2 ]
  200. then
  201. as_user "rm -r $MCSTORE/world2"
  202. fi
  203. fi
  204.  
  205.  
  206. }
  207.  
  208. mc_command() {
  209. command="$1";
  210. if pgrep -u $USERNAME -f $SERVICE > /dev/null
  211. then
  212. pre_log_len=`wc -l "$MCPATH/server.log" | awk '{print $1}'`
  213. echo "$SERVICE is running... executing command"
  214. as_user "screen -p 0 -S minecraft -X eval 'stuff \"$command\"\015'"
  215. sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds
  216. # print output
  217. tail -n $[`wc -l "$MCPATH/server.log" | awk '{print $1}'`-$pre_log_len] "$MCPATH/server.log"
  218. fi
  219. }
  220.  
  221. #Start-Stop here
  222. case "$1" in
  223. start)
  224. mc_start
  225. ;;
  226. stop)
  227. mc_stop
  228. mc_disksavehalt
  229. ;;
  230. restart)
  231. mc_stop
  232. mc_disksavehalt
  233. mc_start
  234. ;;
  235. update)
  236. mc_stop
  237. mc_backup
  238. mc_update
  239. mc_start
  240. ;;
  241. backup)
  242. mc_disksaverun
  243. mc_saveoff
  244. mc_backup
  245. mc_saveon
  246. ;;
  247. disksavehalt)
  248. mc_disksavehalt
  249. ;;
  250. disksaverun)
  251. mc_disksaverun
  252. ;;
  253. status)
  254. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  255. then
  256. echo "$SERVICE is running."
  257. else
  258. echo "$SERVICE is not running."
  259. fi
  260. ;;
  261. command)
  262. if [ $# -gt 1 ]; then
  263. shift
  264. mc_command "$*"
  265. else
  266. echo "Must specify server command (try 'help'?)"
  267. fi
  268. ;;
  269. *)
  270. echo "Usage: /etc/init.d/minecraft {start|stop|update|backup|status|restart|disksaverun|command}"
  271. exit 1
  272. ;;
  273. esac
  274.  
  275. exit 0
RAW Paste Data