gs_starter.sh 6.7 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. return 0
  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 with gs_starter.sh by Paul Klumpp
  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")
  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. if [ "$1" == "" ]; then
  168. echo "Usage: gs_starter.sh <startwatch|stopwatch|stopall|start <instance#>|stop <instance#>>"
  169. return 1
  170. elif [ "$1" == "startwatch" ] || [ "$1" == "startall" ]; then
  171. ACTION="watch"
  172. # check if watcher is already running
  173. if [ -f "gs_starter.run" ]; then
  174. echo "gs_starter.sh is already running. Not starting again."
  175. return 0
  176. else
  177. watcher
  178. return 1
  179. fi
  180. elif [ "$1" == "stopwatch" ]; then
  181. rm "gs_starter.run"
  182. echo "The gs_starter watcher should be stopped now."
  183. return 1
  184. elif [ "$1" == "stopall" ]; then
  185. echo "Stopping all instances."
  186. echo "Pro-Tip: If the watcher is running, the servers will come up again. Useful to restart all instances."
  187. # find all screens with "$PREFIX"
  188. SCRPIDLIST=$(screen -ls | grep -i \.$PREFIX | cut -f1 -d\. | awk {'print $1'})
  189. for x in $SCRPIDLIST; do
  190. kill -9 $x
  191. done
  192. screen -wipe > /dev/null
  193. return 1
  194. elif [ "$1" == "start" ]; then
  195. if [ "$2" != "" ]; then
  196. NUMBER=$2
  197. echo "Starting instance $NUMBER."
  198. echo "Pro-Tip: If you want the watcher to watch this instance, edit gs_starter.cfg parameter ACTIVATE now."
  199. echo " The watcher reloads the config and will begin watching it."
  200. # find all screens with "$PREFIX$NUMBER"
  201. start_instance $NUMBER
  202. return 1
  203. else
  204. echo "Usage: gs_starter.sh start <instance#>"
  205. return 0
  206. fi
  207. elif [ "$1" == "stop" ]; then
  208. if [ "$2" != "" ]; then
  209. NUMBER=$2
  210. echo "Stopping instance $NUMBER."
  211. echo "Pro-Tip: If the watcher is running, it may come up again. So this is useful for restarting with different PARMS."
  212. # find all screens with "$PREFIX$NUMBER"
  213. SCRPID=$(screen -ls | grep -i \.$PREFIX$NUMBER | cut -f1 -d\. | awk {'print $1'})
  214. kill -9 $SCRPID
  215. screen -wipe
  216. return 1
  217. else
  218. echo "Usage: gs_starter.sh stop <instance#>"
  219. return 0
  220. fi
  221. fi
  222. }
  223. main $*
  224. # vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent: