Advertisement
Guest User

Untitled

a guest
Nov 20th, 2011
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 KB | None | 0 0
  1. #!/bin/bash
  2. # /etc/init.d/minecraft
  3.  
  4. ### BEGIN INIT INFO
  5. # Provides: minecraft
  6. # Required-Start: $local_fs $remote_fs
  7. # Required-Stop: $local_fs $remote_fs
  8. # Should-Start: $network
  9. # Should-Stop: $network
  10. # Default-Start: 2 3 4 5
  11. # Default-Stop: 0 1 6
  12. # Short-Description: Minecraft server
  13. # Description: Init script for minecraft/bukkit server, with rolling logs and use of ramdisk for less lag.
  14. ### END INIT INFO
  15.  
  16. # Created by Ahtenus
  17.  
  18. # Based on http://www.minecraftwiki.net/wiki/Server_startup_script
  19. # Support for multiworld by Benni-chan
  20.  
  21.  
  22. #############################
  23. ######### SETTINGS ##########
  24. #############################
  25. # Name of server.jar file
  26. SERVICE="craftbukkit.jar"
  27. # Name to use for the screen instance
  28. SCREEN="gara"
  29. # User that should run the server
  30. USERNAME="minecraft"
  31. # Path to minecraft directory
  32. MCPATH="/home/minecraft"
  33. # Number of CPUs/cores to use
  34. CPU_COUNT=1
  35. # Initial memory usage
  36. INITMEM="512M"
  37. # Maximum amount of memory to use
  38. MAXMEM="1536M"
  39. # Remember: give the ramdisk enough space, subtract from the total amount
  40. # of RAM available the size of your map and the RAM-consumption of your base system.
  41. INVOCATION="java -Xmx$MAXMEM -Xms$INITMEM -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar $SERVICE nogui"
  42. # Where the world backups should go
  43. BACKUPPATH="/home/minecraft/minebak"
  44. # Where the logs are copied with log-roll
  45. LOGPATH="${MCPATH}/logs"
  46. # Where the whole minecraft directory is copied when whole-backup is executed
  47. WHOLEBACKUP="/home/minecraft/serverbak"
  48. # Where the worlds are located on the disk. Can not be the same as MCPATH.
  49. WORLDSTORAGE="${MCPATH}/worldstorage"
  50. # Path to the the mounted ramdisk default in Ubuntu: /dev/shm
  51. RAMDISK="/dev/shm"
  52. # use tar or zip files for world backup
  53. BACKUPFORMAT="tar"
  54.  
  55. ME=`whoami`
  56. as_user() {
  57. if [ $ME == $USERNAME ] ; then
  58. bash -c "$1"
  59. else
  60. su - $USERNAME -c "$1"
  61. fi
  62. }
  63. datepath() {
  64. # datepath path filending-to-check returned-filending
  65. if [ -e $1`date +%F`$2 ]
  66. then
  67. echo $1`date +%FT%T`$3
  68. else
  69. echo $1`date +%F`$3
  70. fi
  71. }
  72. mc_start() {
  73. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  74. then
  75. echo "Tried to start but $SERVICE was already running!"
  76. else
  77. echo "$SERVICE was not running... starting."
  78. cd $MCPATH
  79. as_user "cd $MCPATH && screen -dmS $SCREEN $INVOCATION"
  80. sleep 7
  81. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  82. then
  83. echo "$SERVICE is now running."
  84. else
  85. echo "Could not start $SERVICE."
  86. fi
  87. fi
  88. }
  89.  
  90. mc_saveoff() {
  91. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  92. then
  93. echo "$SERVICE is running... suspending saves"
  94. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-off\"\015'"
  95. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-all\"\015'"
  96. sync
  97. sleep 10
  98. else
  99. echo "$SERVICE was not running. Not suspending saves."
  100. fi
  101. }
  102.  
  103. mc_saveon() {
  104. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  105. then
  106. echo "$SERVICE is running... re-enabling saves"
  107. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-on\"\015'"
  108. else
  109. echo "$SERVICE was not running. Not resuming saves."
  110. fi
  111. }
  112.  
  113. mc_stop() {
  114. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  115. then
  116. echo "$SERVICE is running... stopping."
  117. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-all\"\015'"
  118. sleep 10
  119. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"stop\"\015'"
  120. sleep 7
  121. else
  122. echo "$SERVICE was not running."
  123. fi
  124. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  125. then
  126. echo "$SERVICE could not be shut down... still running."
  127. else
  128. echo "$SERVICE is shut down."
  129. fi
  130. }
  131. log_roll() {
  132. as_user "mkdir -p $LOGPATH"
  133. path=`datepath $LOGPATH/server_ .log.gz .log`
  134. as_user "mv $MCPATH/server.log $path && gzip $path"
  135. }
  136. get_worlds() {
  137. a=1
  138. for NAME in $(ls $WORLDSTORAGE)
  139. do
  140. if [ -d $WORLDSTORAGE/$NAME ]
  141. then
  142. WORLDNAME[$a]=$NAME
  143. if [ -e $WORLDSTORAGE/$NAME/ramdisk ]
  144. then
  145. WORLDRAM[$a]=true
  146. else
  147. WORLDRAM[$a]=false
  148. fi
  149. a=$a+1
  150. fi
  151. done
  152. }
  153. mc_whole_backup() {
  154. as_user "mkdir -p $WHOLEBACKUP"
  155. path=`datepath $WHOLEBACKUP/mine_`
  156. as_user "cp -rP $MCPATH $path"
  157. }
  158. mc_world_backup() {
  159. get_worlds
  160. for INDEX in ${!WORLDNAME[@]}
  161. do
  162. echo "Backing up minecraft ${WORLDNAME[$INDEX]}"
  163. as_user "mkdir -p $BACKUPPATH"
  164.  
  165. case "$BACKUPFORMAT" in
  166. tar)
  167. path=`datepath $BACKUPPATH/${WORLDNAME[$INDEX]}_ .tar.bz2 .tar.bz2`
  168. as_user "tar -hcjf $path $MCPATH/${WORLDNAME[$INDEX]}"
  169. ;;
  170. zip)
  171. path=`datepath $BACKUPPATH/${WORLDNAME[$INDEX]}_ .zip .zip`
  172. as_user "zip -rq $path $MCPATH/${WORLDNAME[$INDEX]}"
  173. ;;
  174. *)
  175. echo "$BACKUPFORMAT is no supported backup format"
  176. ;;
  177. esac
  178. done
  179. }
  180. check_links() {
  181. get_worlds
  182. for INDEX in ${!WORLDNAME[@]}
  183. do
  184. if [[ -L $MCPATH/${WORLDNAME[$INDEX]} || ! -a $MCPATH/${WORLDNAME[$INDEX]} ]]
  185. then
  186. link=`ls -l $MCPATH/${WORLDNAME[$INDEX]} | awk '{print $11}'`
  187. if ${WORLDRAM[$INDEX]}
  188. then
  189. if [ "$link" != "$RAMDISK/${WORLDNAME[$INDEX]}" ]
  190. then
  191. as_user "rm -f $MCPATH/${WORLDNAME[$INDEX]}"
  192. as_user "ln -s $RAMDISK/${WORLDNAME[$INDEX]} $MCPATH/${WORLDNAME[$INDEX]}"
  193. echo "created link for ${WORLDNAME[$INDEX]}"
  194. fi
  195. else
  196. if [ "$link" != "${WORLDSTORAGE}/${WORLDNAME[$INDEX]}" ]
  197. then
  198. as_user "rm -f $MCPATH/${WORLDNAME[$INDEX]}"
  199. as_user "ln -s ${WORLDSTORAGE}/${WORLDNAME[$INDEX]} $MCPATH/${WORLDNAME[$INDEX]}"
  200. echo "created link for ${WORLDNAME[$INDEX]}"
  201. fi
  202. fi
  203. else
  204. echo "could not process ${WORLDNAME[$INDEX]}. please move all worlds to ${WORLDSTORAGE}."
  205. exit 1
  206. fi
  207. done
  208. echo "links checked"
  209. }
  210. to_ram() {
  211. get_worlds
  212. for INDEX in ${!WORLDNAME[@]}
  213. do
  214. if ${WORLDRAM[$INDEX]}
  215. then
  216. if [ -L $MCPATH/${WORLDNAME[$INDEX]} ]
  217. then
  218. as_user "rsync -rt --exclude 'ramdisk' ${WORLDSTORAGE}/${WORLDNAME[$INDEX]}/ $RAMDISK/${WORLDNAME[$INDEX]}"
  219. echo "${WORLDNAME[$INDEX]} copied to ram"
  220. fi
  221. fi
  222. done
  223. }
  224. to_disk() {
  225. get_worlds
  226. for INDEX in ${!WORLDNAME[@]}
  227. do
  228. as_user "rsync -rt --exclude 'ramdisk' $MCPATH/${WORLDNAME[$INDEX]}/ ${WORLDSTORAGE}/${WORLDNAME[$INDEX]}"
  229. echo "${WORLDNAME[$INDEX]} copied to disk"
  230. done
  231. }
  232. mc_update() {
  233. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  234. then
  235. echo "$SERVICE is running! Will not start update."
  236. else
  237.  
  238. ### update craftbukkit
  239.  
  240. echo "Updating craftbukkit...."
  241. as_user "cd $MCPATH && wget -q -O $MCPATH/craftbukkit.jar.update http://ci.bukkit.org/job/dev-CraftBukkit/promotion/latest/Recommended/artifact/target/craftbukkit-0.0.1-SNAPSHOT.jar"
  242. if [ -f $MCPATH/craftbukkit.jar.update ]
  243. then
  244. if `diff $MCPATH/craftbukkit-0.0.1-SNAPSHOT.jar $MCPATH/craftbukkit.jar.update > /dev/null`
  245. then
  246. echo "You are already running the latest version of CraftBukkit."
  247. as_user "rm $MCPATH/craftbukkit.jar.update"
  248. else
  249. as_user "mv $MCPATH/craftbukkit.jar.update $MCPATH/craftbukkit-0.0.1-SNAPSHOT.jar"
  250. echo "CraftBukkit successfully updated."
  251. fi
  252. else
  253. echo "CraftBukkit update could not be downloaded."
  254. fi
  255. fi
  256. }
  257. change_ramdisk_state() {
  258. if [ ! -e $WORLDSTORAGE/$1 ]
  259. then
  260. echo "World \"$1\" not found."
  261. exit 1
  262. fi
  263. if [ -e $WORLDSTORAGE/$1/ramdisk ]
  264. then
  265. rm $WORLDSTORAGE/$1/ramdisk
  266. echo "removed ramdisk flag from \"$1\""
  267. else
  268. touch $WORLDSTORAGE/$1/ramdisk
  269. echo "added ramdisk flag to \"$1\""
  270. fi
  271. echo "changes will only take effect after server is restarted."
  272. }
  273.  
  274.  
  275. case "$1" in
  276. start)
  277. # Starts the server
  278. check_links
  279. to_ram
  280. mc_start
  281. ;;
  282. stop)
  283. # Stops the server
  284. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say SERVER SHUTTING DOWN!\"\015'"
  285. mc_stop
  286. to_disk
  287. ;;
  288. restart)
  289. # Restarts the server
  290. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say SERVER REBOOT IN 10 SECONDS.\"\015'"
  291. mc_stop
  292. to_disk
  293. check_links
  294. to_ram
  295. mc_start
  296. ;;
  297. backup)
  298. # Backups world
  299. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say Backing up world.\"\015'"
  300. mc_saveoff
  301. to_disk
  302. mc_world_backup
  303. mc_saveon
  304. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say Backup complete.\"\015'"
  305. ;;
  306. whole-backup)
  307. # Backup everything
  308. mc_whole_backup
  309. ;;
  310. update)
  311. #update minecraft_server.jar and craftbukkit.jar (thanks karrth)
  312. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say SERVER UPDATE IN 10 SECONDS.\"\015'"
  313. mc_stop
  314. to_disk
  315. mc_whole_backup
  316. mc_update
  317. check_links
  318. mc_start
  319. ;;
  320. to-disk)
  321. # Writes from the ramdisk to disk, in case the server crashes.
  322. # Using ramdisk speeds things up a lot, especially if you allow
  323. # teleportation on the server.
  324. mc_saveoff
  325. to_disk
  326. mc_saveon
  327. ;;
  328. connected)
  329. # Lists connected users
  330. as_user "screen -p 0 -S $SCREEN -X eval 'stuff list\015'"
  331. sleep 3s
  332. tac $MCPATH/server.log | grep -m 1 "Connected"
  333. ;;
  334. log-roll)
  335. # Moves and Gzips the logfile, a big log file slows down the
  336. # server A LOT (what was notch thinking?)
  337. as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say ROUTINE REBOOT IN 10 SECONDS.\"\015'"
  338. mc_stop
  339. log_roll
  340. mc_start
  341. ;;
  342. last)
  343. # greps for recently logged in users
  344. echo Recently logged in users:
  345. cat $MCPATH/server.log | awk '/entity|conn/ {sub(/lost/,"disconnected");print $1,$2,$4,$5}'
  346. ;;
  347. status)
  348. # Shows server status
  349. if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
  350. then
  351. echo "$SERVICE is running."
  352. else
  353. echo "$SERVICE is not running."
  354. fi
  355. ;;
  356. version)
  357. echo Craftbukkit version `awk '/Craftbukkit/ {sub(/\)/, ""); print $12}' $MCPATH/server.log`
  358. ;;
  359. links)
  360. check_links
  361. ;;
  362. ramdisk)
  363. change_ramdisk_state $2
  364. ;;
  365. worlds)
  366. get_worlds
  367. for INDEX in ${!WORLDNAME[@]}
  368. do
  369. if ${WORLDRAM[$INDEX]}
  370. then
  371. echo "${WORLDNAME[$INDEX]} (ramdisk)"
  372. else
  373. echo ${WORLDNAME[$INDEX]}
  374. fi
  375. done
  376. ;;
  377. help)
  378. echo "Usage: /etc/init.d/minecraft command"
  379. echo
  380. echo "start - Starts the server"
  381. echo "stop - stops the server"
  382. echo "restart - restarts the server"
  383. echo "backup - backups the worlds defined in the script"
  384. echo "whole-backup - backups the entire server folder"
  385. echo "update - fetches the latest version of minecraft.jar server and Bukkit"
  386. echo "log-roll - Moves and gzips the logfile"
  387. echo "to-disk - copies the worlds from the ramdisk to worldstorage"
  388. echo "connected - lists connected users"
  389. echo "status - Shows server status"
  390. echo "version - returs Bukkit version"
  391. echo "links - creates nessesary symlinks"
  392. echo "last - shows recently connected users"
  393. echo "worlds - shows a list of available worlds"
  394. echo "ramdisk WORLD - toggles ramdisk configuration for WORLD"
  395. ;;
  396. *)
  397. echo "No such command see /etc/init.d/minecraft help"
  398. exit 1
  399. ;;
  400. esac
  401.  
  402. exit 0
  403.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement