gs_starter.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. # vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
  20. #
  21. # bash is needed!!
  22. #
  23. # by Paul Klumpp, 2012-11-14
  24. #
  25. # gs_starter.cfg is part of gs_starter.sh
  26. #
  27. ##### now, hands away... #####
  28. function loadcfg() {
  29. if [ -f "gs_starter.cfg" ]; then
  30. . "gs_starter.cfg"
  31. else
  32. echo "The gs_starter.cfg is not there! Won't work without! Exiting"
  33. return 0
  34. fi
  35. for idx in ${!ACTIVATE[*]}; do
  36. SCREEN[${ACTIVATE[$idx]}]="${PREFIX}${ACTIVATE[$idx]}"
  37. done
  38. #echo ${ACTIVATE[*]} vs ${OLD_ACTIVATE[*]}
  39. if [ "${ACTIVATE[*]}" != "${OLD_ACTIVATE[*]}" ]; then
  40. if [ "$1" != "first" ]; then
  41. echo "Lets keep those activated: ${SCREEN[*]}"
  42. fi
  43. fi
  44. OLD_ACTIVATE=${ACTIVATE[*]}
  45. }
  46. s_lib=""
  47. function start_instance() {
  48. PARMS=${PARMS[$1]}
  49. if [ "$PARMS" != "" ]; then
  50. # if it still runs, don't start again
  51. SCR=${PREFIX}$1
  52. if [ "$SCR" != "" ]; then
  53. printf "."
  54. fi
  55. running=$(screen -list | grep -vi "dead" | grep -i "$SCR" | awk {'print $1'} | wc -l)
  56. if [ $running -eq 0 ]; then
  57. #start it daemonized via screen
  58. shellscript="${PREFIX}-$1.sh"
  59. cat > $shellscript <<here-doc
  60. #!/usr/bin/env bash
  61. #file created with gs_starter.sh by Paul Klumpp
  62. while true; do
  63. echo
  64. echo "Starting '${GSDED} ${PARMS}' $s_lib"
  65. echo
  66. LD_LIBRARY_PATH=${s_lib} ./${GSDED} ${PARMS}
  67. echo
  68. echo "----sleep---- ctrl+c to hit the break :-)"
  69. echo
  70. sleep 3
  71. done
  72. here-doc
  73. chmod u+x $shellscript
  74. echo "Starting '${GSDED} ${PARMS}' $s_lib"
  75. screen -dm -S "${SCR}" "./$shellscript"
  76. echo "on screen: ${SCR}"
  77. fi
  78. fi
  79. }
  80. function find_filebits {
  81. Q64=$(file "${1}" | grep -i "x86-64" | wc -l)
  82. if [ "${Q64}" == "1" ]; then
  83. echo 64
  84. return 1
  85. fi
  86. Q32=$(file "${1}" | grep -i "32-bit" | wc -l)
  87. if [ "${Q32}" == "1" ]; then
  88. echo 32
  89. return 1
  90. fi
  91. # fallback and assume system bits
  92. sb=find_sysbits
  93. echo $sb
  94. return 1
  95. }
  96. function find_sysbits {
  97. S64=$(uname -a | grep -i "x86_64" | wc -l)
  98. if [ "${S64}" == "1" ]; then
  99. echo 64
  100. else
  101. echo 32
  102. fi
  103. }
  104. function f_realpath() {
  105. RPBIN=`which realpath`
  106. if [ -x "${RPBIN}" ]; then
  107. echo $(realpath "${1}")
  108. else
  109. echo ${1}
  110. fi
  111. }
  112. function control_c {
  113. if [ -f "gs_starter.run" ]; then
  114. rm "gs_starter.run"
  115. fi
  116. exit $?
  117. }
  118. function watcher {
  119. echo "Watcher begins..."
  120. # trap keyboard interrupt (control-c)
  121. trap control_c SIGINT
  122. echo "watcher runs" > "gs_starter.run"
  123. echo "Lets keep those activated: ${SCREEN[*]}"
  124. # start them..
  125. while [ -f "gs_starter.run" ]; do
  126. for index in ${!ACTIVATE[*]}
  127. do
  128. #start, if not running .. checks if running are in start_instance
  129. start_instance ${ACTIVATE[$index]}
  130. done
  131. sleep 4
  132. loadcfg
  133. done
  134. }
  135. function main {
  136. loadcfg first
  137. RPQ=$(f_realpath "$GSDED")
  138. FBITS=$(find_filebits $RPQ)
  139. SBITS=$(find_sysbits)
  140. #echo fbits: $FBITS
  141. #echo sbits: $SBITS
  142. if [ $SBITS -gt $FBITS ]; then
  143. # link to 32bit libs
  144. s_lib="lib32/"
  145. fi
  146. if [ $SBITS -lt $FBITS ]; then
  147. echo "Can't start 64 bit executables ($GSDED) on this system."
  148. return 0
  149. fi
  150. if [ "$1" == "" ]; then
  151. echo "Usage: gs_starter.sh <startwatch|stopwatch|stopall|start <instance#>|stop <instance#>>"
  152. return 1
  153. elif [ "$1" == "startwatch" ] || [ "$1" == "startall" ]; then
  154. ACTION="watch"
  155. # check if watcher is already running
  156. if [ -f "gs_starter.run" ]; then
  157. echo "gs_starter.sh is already running. Not starting again."
  158. return 0
  159. else
  160. watcher
  161. return 1
  162. fi
  163. elif [ "$1" == "stopwatch" ]; then
  164. rm "gs_starter.run"
  165. echo "The gs_starter watcher should be stopped now."
  166. return 1
  167. elif [ "$1" == "stopall" ]; then
  168. echo "Stopping all instances."
  169. echo "Pro-Tip: If the watcher is running, the servers will come up again. Useful to restart all instances."
  170. # find all screens with "$PREFIX"
  171. SCRPIDLIST=$(screen -ls | grep -i \.$PREFIX | cut -f1 -d\. | awk {'print $1'})
  172. for x in $SCRPIDLIST; do
  173. kill -9 $x
  174. done
  175. screen -wipe > /dev/null
  176. return 1
  177. elif [ "$1" == "start" ]; then
  178. if [ "$2" != "" ]; then
  179. NUMBER=$2
  180. echo "Starting instance $NUMBER."
  181. echo "Pro-Tip: If you want the watcher to watch this instance, edit gs_starter.cfg parameter ACTIVATE now."
  182. echo " The watcher reloads the config and will begin watching it."
  183. # find all screens with "$PREFIX$NUMBER"
  184. start_instance $NUMBER
  185. return 1
  186. else
  187. echo "Usage: gs_starter.sh start <instance#>"
  188. return 0
  189. fi
  190. elif [ "$1" == "stop" ]; then
  191. if [ "$2" != "" ]; then
  192. NUMBER=$2
  193. echo "Stopping instance $NUMBER."
  194. echo "Pro-Tip: If the watcher is running, it may come up again. So this is useful for restarting with different PARMS."
  195. # find all screens with "$PREFIX$NUMBER"
  196. SCRPID=$(screen -ls | grep -i \.$PREFIX$NUMBER | cut -f1 -d\. | awk {'print $1'})
  197. kill -9 $SCRPID
  198. screen -wipe
  199. return 1
  200. else
  201. echo "Usage: gs_starter.sh stop <instance#>"
  202. return 0
  203. fi
  204. fi
  205. }
  206. main $*