gs_starter.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/usr/bin/env bash
  2. # gs_starter.sh - Tool for managing unix screens with bash. Typically game servers.
  3. # ----------------------------------------------------------------
  4. # Copyright (C) 2012 Paul-Dieter Klumpp
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # ----------------------------------------------------------------
  19. #
  20. # bash is needed!!
  21. #
  22. # by Paul Klumpp, 2012-11-14
  23. #
  24. # gs_starter.cfg is part of gs_starter.sh
  25. #
  26. ##### now, hands away... #####
  27. if [ "${SHELL}" != "$(which bash)" ]; then
  28. echo "Start this script with bash!"
  29. exit
  30. fi
  31. function loadcfg() {
  32. if [ -f "gs_starter.cfg" ]; then
  33. . "./gs_starter.cfg"
  34. else
  35. echo "The gs_starter.cfg is not there! Won't work without! Exiting"
  36. return 0
  37. fi
  38. for idx in ${!ACTIVATE[*]}; do
  39. SCREEN[${ACTIVATE[$idx]}]="${PREFIX}${ACTIVATE[$idx]}"
  40. done
  41. #echo ${ACTIVATE[*]} vs ${OLD_ACTIVATE[*]}
  42. if [ "${ACTIVATE[*]}" != "${OLD_ACTIVATE[*]}" ]; then
  43. if [ "$1" != "first" ]; then
  44. echo "Lets keep those activated: ${SCREEN[*]}"
  45. fi
  46. fi
  47. OLD_ACTIVATE=${ACTIVATE[*]}
  48. }
  49. s_lib=""
  50. function start_instance() {
  51. PARMS=${PARMS[$1]}
  52. if [ "$PARMS" != "" ]; then
  53. # if it still runs, don't start again
  54. SCR=${PREFIX}$1
  55. if [ "$SCR" != "" ]; then
  56. printf "."
  57. fi
  58. running=$(screen -list | grep -vi "dead" | grep -i "$SCR" | awk {'print $1'} | wc -l)
  59. if [ $running -eq 0 ]; then
  60. #start it daemonized via screen
  61. shellscript="${PREFIX}-$1.sh"
  62. cat > $shellscript <<here-doc
  63. #!/usr/bin/env bash
  64. #file created by gs_starter.sh - Don't edit. Your changes will be overwritten!
  65. while true; do
  66. echo
  67. echo "Starting '${GSDED} ${PARMS}' $s_lib"
  68. echo
  69. LD_LIBRARY_PATH=${s_lib} ./${GSDED} ${PARMS}
  70. echo
  71. echo "----sleep---- ctrl+c to hit the break :-)"
  72. echo
  73. sleep 3
  74. done
  75. here-doc
  76. chmod u+x $shellscript
  77. echo "Starting '${GSDED} ${PARMS}' $s_lib"
  78. screen -dm -S "${SCR}" "./$shellscript"
  79. echo "on screen: ${SCR}"
  80. fi
  81. fi
  82. }
  83. function find_filebits {
  84. Q64=$(file "${1}" | grep -i "x86-64" | wc -l)
  85. if [ "${Q64}" == "1" ]; then
  86. echo 64
  87. return 1
  88. fi
  89. Q32=$(file "${1}" | grep -i "32-bit" | wc -l)
  90. if [ "${Q32}" == "1" ]; then
  91. echo 32
  92. return 1
  93. fi
  94. # fallback and assume system bits
  95. sb=$(find_sysbits)
  96. echo $sb
  97. return 1
  98. }
  99. function find_sysbits {
  100. S64=$(uname -a | grep -i "x86_64" | wc -l)
  101. if [ "${S64}" == "1" ]; then
  102. echo 64
  103. else
  104. echo 32
  105. fi
  106. }
  107. function f_realpath() {
  108. RPBIN=$(which realpath)
  109. if [ -x "${RPBIN}" ]; then
  110. echo $(realpath "${1}")
  111. else
  112. echo ${1}
  113. fi
  114. }
  115. function control_c {
  116. if [ -f "gs_starter.run" ]; then
  117. rm "gs_starter.run"
  118. fi
  119. exit $?
  120. }
  121. function watcher {
  122. echo "Watcher begins..."
  123. # trap keyboard interrupt (control-c)
  124. trap control_c SIGINT
  125. echo "watcher runs" > "gs_starter.run"
  126. echo "Lets keep those activated: ${SCREEN[*]}"
  127. # start them..
  128. while [ -f "gs_starter.run" ]; do
  129. for index in ${!ACTIVATE[*]}
  130. do
  131. #start, if not running .. checks if running are in start_instance
  132. start_instance ${ACTIVATE[$index]}
  133. done
  134. sleep 4
  135. loadcfg
  136. done
  137. }
  138. function checkinstalled {
  139. if [ "$1" != "" ]; then
  140. I=$(which "$1" 2> /dev/null)
  141. if [ -f "$I" ]; then
  142. echo 1
  143. else
  144. echo 0
  145. fi
  146. fi
  147. }
  148. function main {
  149. loadcfg first
  150. if [ "$(checkinstalled "realpath")" == "1" ]; then
  151. RPQ=$(realpath "$GSDED")
  152. else
  153. RPQ=$GSDED
  154. fi
  155. FBITS=$(find_filebits $RPQ)
  156. SBITS=$(find_sysbits)
  157. #echo fbits: $FBITS
  158. #echo sbits: $SBITS
  159. #if [ $SBITS -gt $FBITS ]; then
  160. # # link to 32bit libs
  161. # s_lib="lib32/"
  162. #fi
  163. if [ $SBITS -lt $FBITS ]; then
  164. echo "Can't start 64 bit executables ($GSDED) on this system."
  165. return 0
  166. fi
  167. #lib dir for ppl
  168. s_lib="LD_LIBRARY_PATH=libs/:$LD_LIBRARY_PATH"
  169. if [ "$1" == "" ]; then
  170. echo "Usage: gs_starter.sh <startwatch|stopwatch|stopall|start <instance#>|stop <instance#>>"
  171. return 1
  172. elif [ "$1" == "startwatch" ] || [ "$1" == "startall" ]; then
  173. ACTION="watch"
  174. # check if watcher is already running
  175. if [ -f "gs_starter.run" ]; then
  176. echo "gs_starter.sh is already running. Not starting again."
  177. return 0
  178. else
  179. watcher
  180. return 1
  181. fi
  182. elif [ "$1" == "stopwatch" ]; then
  183. rm "gs_starter.run"
  184. echo "The gs_starter watcher should be stopped now."
  185. return 1
  186. elif [ "$1" == "stopall" ]; then
  187. echo "Stopping all instances."
  188. echo "Pro-Tip: If the watcher is running, the servers will come up again. Useful to restart all instances."
  189. # find all screens with "$PREFIX"
  190. SCRPIDLIST=$(screen -ls | grep -i \.$PREFIX | cut -f1 -d\. | awk {'print $1'})
  191. for x in $SCRPIDLIST; do
  192. kill -9 $x
  193. done
  194. screen -wipe > /dev/null
  195. return 1
  196. elif [ "$1" == "start" ]; then
  197. if [ "$2" != "" ]; then
  198. NUMBER=$2
  199. echo "Starting instance $NUMBER."
  200. echo "Pro-Tip: If you want the watcher to watch this instance, edit gs_starter.cfg parameter ACTIVATE now."
  201. echo " The watcher reloads the config and will begin watching it."
  202. # find all screens with "$PREFIX$NUMBER"
  203. start_instance $NUMBER
  204. return 1
  205. else
  206. echo "Usage: gs_starter.sh start <instance#>"
  207. return 0
  208. fi
  209. elif [ "$1" == "stop" ]; then
  210. if [ "$2" != "" ]; then
  211. NUMBER=$2
  212. echo "Stopping instance $NUMBER."
  213. echo "Pro-Tip: If the watcher is running, it may come up again. So this is useful for restarting with different PARMS."
  214. # find all screens with "$PREFIX$NUMBER"
  215. SCRPID=$(screen -ls | grep -i \.$PREFIX$NUMBER | cut -f1 -d\. | awk {'print $1'})
  216. kill -9 $SCRPID
  217. screen -wipe
  218. return 1
  219. else
  220. echo "Usage: gs_starter.sh stop <instance#>"
  221. return 0
  222. fi
  223. fi
  224. }
  225. main $*
  226. # vim: tabstop=2 shiftwidth=2 softtabstop=2 autoindent:
  227. # kate: space-indent off; indent-width 2; mixedindent off;