Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. #!/bin/bash
  2. # /etc/init.d/vintagestory.sh
  3. # version 0.4.2 2016-02-09 (YYYY-MM-DD)
  4. #
  5. ### BEGIN INIT INFO
  6. # Provides: vintagestory
  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: vintagestory server
  14. # Description: Starts the vintagestory server
  15. ### END INIT INFO
  16.  
  17. # Shamelessly adapted from http://minecraft.gamepedia.com/Tutorials/Server_startup_script
  18.  
  19. #Settings
  20. SERVICE='VintagestoryServer.exe'
  21. SCREENNAME='vintagestory_server'
  22. OPTIONS=''
  23. USERNAME='vintagestory'
  24. WORLD='world'
  25. HISTORY=1024
  26. VSPATH='/home/vintagestory/server'
  27. INVOCATION="mono $SERVICE $OPTIONS"
  28.  
  29. #Commands
  30. command -v mono >/dev/null 2>&1 || { echo "I require mono but it's not installed. Aborting." >&2; exit 1; }
  31. command -v monodis >/dev/null 2>&1 || { echo "I require monodis but it's not installed. Aborting." >&2; exit 1; }
  32. command -v curl >/dev/null 2>&1 || { echo "I require curl but it's not installed. Aborting." >&2; exit 1; }
  33. command -v wget >/dev/null 2>&1 || { echo "I require wget but it's not installed. Aborting." >&2; exit 1; }
  34. command -v screen >/dev/null 2>&1 || { echo "I require screen but it's not installed. Aborting." >&2; exit 1; }
  35.  
  36. ME=`whoami`
  37. as_user() {
  38. if [ "$ME" = "$USERNAME" ] ; then
  39. bash -c "$1"
  40. else
  41. su - "$USERNAME" -c "$1"
  42. fi
  43. }
  44.  
  45. vs_start() {
  46. if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
  47. echo "$SERVICE is already running!"
  48. else
  49. echo "Starting $SERVICE..."
  50. cd $VSPATH
  51. as_user "cd $VSPATH && screen -h $HISTORY -dmS ${SCREENNAME} $INVOCATION"
  52. sleep 7
  53. if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
  54. echo "$SERVICE is now running."
  55. else
  56. echo "Error! Could not start $SERVICE!"
  57. fi
  58. fi
  59. }
  60.  
  61. vs_stop() {
  62. if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
  63. echo "Stopping $SERVICE"
  64. as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
  65. sleep 10
  66. as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"/stop\"\015'"
  67. sleep 7
  68. else
  69. echo "$SERVICE was not running."
  70. fi
  71. if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
  72. echo "Error! $SERVICE could not be stopped."
  73. else
  74. echo "$SERVICE is stopped."
  75. fi
  76. }
  77.  
  78. vs_command() {
  79. command="$1";
  80. if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
  81. pre_log_len=`wc -l "$VSPATH/VintagestoryData/Logs/server-main.txt" | awk '{print $1}'`
  82. echo "$SERVICE is running... executing command"
  83. as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"/$command\"\015'"
  84. sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds
  85. # print output
  86. tail -n $[`wc -l "$VSPATH/VintagestoryData/Logs/server-main.txt" | awk '{print $1}'`-$pre_log_len] "$VSPATH/VintagestoryData/Logs/server-main.txt"
  87. fi
  88. }
  89.  
  90. vercomp () {
  91. if [[ $1 == $2 ]]
  92. then
  93. return 0
  94. fi
  95. local IFS=.
  96. local i ver1=($1) ver2=($2)
  97. # fill empty fields in ver1 with zeros
  98. for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
  99. do
  100. ver1[i]=0
  101. done
  102. for ((i=0; i<${#ver1[@]}; i++))
  103. do
  104. if [[ -z ${ver2[i]} ]]
  105. then
  106. # fill empty fields in ver2 with zeros
  107. ver2[i]=0
  108. fi
  109. if ((10#${ver1[i]} > 10#${ver2[i]}))
  110. then
  111. return 1
  112. fi
  113. if ((10#${ver1[i]} < 10#${ver2[i]}))
  114. then
  115. return 2
  116. fi
  117. done
  118. return 0
  119. }
  120. testvercomp () {
  121. vercomp $1 $2
  122. case $? in
  123. 0) op='=';;
  124. 1) op='>';;
  125. 2) op='<';;
  126. esac
  127. if [[ $op == '=' ]]
  128. then
  129. echo "Your Vintage Story installation is already newest version $2"
  130. exit
  131. else
  132. echo "Update"
  133. fi
  134. }
  135. vs_update () {
  136. cd $VSPATH
  137. oldversion=$(monodis --assembly VintagestoryLib.dll | grep Version | cut -c10-)
  138. newversion=$(wget http://version.vintagestory.at/version.txt -q -O -)
  139. testvercomp $oldversion $newversion '<'
  140. vs_install $newversion
  141. installedversion=$(monodis --assembly VintagestoryLib.dll | grep Version | cut -c10-)
  142. echo Finished update to $installedversion
  143. vs_start
  144. }
  145. vs_reinstall () {
  146. newversion=$(monodis --assembly VintagestoryLib.dll | grep Version | cut -c10-)
  147. vs_install $newversion
  148. echo Finished reinstall to $newversion
  149. vs_start
  150. }
  151. vs_install () {
  152. cd $VSPATH
  153. newversion=$(echo $1 | cut -c-5)
  154. echo Start downloading
  155. curl http://account.vintagestory.at/files/vs_server_$newversion.tar.gz -o vs_server_$newversion.tar.gz -L
  156. tar -xzf vs_server_$newversion.tar.gz
  157. vs_command "/announce Short restart for a Vintage Story update"
  158. vs_stop
  159. vs_backup
  160. rm -rf vs_server_$newversion.tar.gz
  161. sed -i "s/USERNAME='vintagestory'/USERNAME='$USERNAME'/" server.sh
  162. SEDVSPATH="$(echo $VSPATH | sed 's/\//\\\//g')"
  163. sed -i "s/VSPATH='\/home\/vintagestory\/server'/VSPATH='$SEDVSPATH'/" server.sh
  164. chmod 755 server.sh
  165. }
  166.  
  167. vs_backup () {
  168. cd $VSPATH
  169. version=$(monodis --assembly VintagestoryLib.dll | grep Version | cut -c16-)
  170. if [ ! -d "backup" ]; then
  171. mkdir "backup"
  172. fi
  173. echo Start backup
  174. aktualtime=$(date +%d.%m.%y-%R)
  175. tar czf backup/vs-server-$aktualtime-v$version.tar.gz * --exclude=backup
  176. echo Finished backup to vs-archiv-$aktualtime-v$version.tar.gz
  177. }
  178. case "$1" in
  179. start)
  180. vs_start
  181. ;;
  182. stop)
  183. vs_stop
  184. ;;
  185. restart)
  186. vs_stop
  187. vs_start
  188. ;;
  189. update)
  190. vs_update
  191. ;;
  192. backup)
  193. vs_backup
  194. ;;
  195. reinstall)
  196. vs_reinstall
  197. ;;
  198. status)
  199. if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
  200. echo "$SERVICE is running."
  201. else
  202. echo "$SERVICE is not running."
  203. fi
  204. ;;
  205. command)
  206. if [ $# -gt 1 ] ; then
  207. shift
  208. vs_command "$*"
  209. else
  210. echo "Must specify server command (try 'help'?)"
  211. fi
  212. ;;
  213.  
  214. *)
  215. echo "Usage: $0 {start|stop|status|restart|update|backup|reinstall|command \"server command\"}"
  216. exit 1
  217. ;;
  218. esac
  219.  
  220. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement