gs_starter.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 [ -z "${BASH_VERSION}" ]; 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 in ${GSPATH}"
  68. echo
  69. cd ${GSPATH}
  70. LD_LIBRARY_PATH=${s_lib} ./${GSDED} ${PARMS}
  71. echo
  72. echo "----sleep---- ctrl+c to hit the break :-)"
  73. echo
  74. sleep 3
  75. done
  76. here-doc
  77. chmod u+x $shellscript
  78. echo "Starting '${GSDED} ${PARMS}' $s_lib"
  79. screen -dm -S "${SCR}" "./$shellscript"
  80. echo "on screen: ${SCR}"
  81. if [ "$RENICE" == "true" ]; then
  82. if [ ! -z "${NICE[$1]}" ]; then
  83. sleep 1s
  84. NICE=${NICE[$1]}
  85. PROCESS=$(ps -U $USER -o pid,cmd | grep -v grep | grep -i "${GSDED} ${PARMS}")
  86. echo process:$PROCESS
  87. PID=$(echo $PROCESS | awk {'print $1'})
  88. echo pid:$PID
  89. sudo renice --priority ${NICE} --pid $PID
  90. fi
  91. fi
  92. fi
  93. fi
  94. }
  95. function find_filebits {
  96. Q64=$(file "${1}" | grep -i "x86-64" | wc -l)
  97. if [ "${Q64}" == "1" ]; then
  98. echo 64
  99. return 1
  100. fi
  101. Q32=$(file "${1}" | grep -i "32-bit" | wc -l)
  102. if [ "${Q32}" == "1" ]; then
  103. echo 32
  104. return 1
  105. fi
  106. # fallback and assume system bits
  107. sb=$(find_sysbits)
  108. echo $sb
  109. return 1
  110. }
  111. function find_sysbits {
  112. S64=$(uname -a | grep -i "x86_64" | wc -l)
  113. if [ "${S64}" == "1" ]; then
  114. echo 64
  115. else
  116. echo 32
  117. fi
  118. }
  119. function f_realpath() {
  120. RPBIN=$(which realpath)
  121. if [ -x "${RPBIN}" ]; then
  122. echo $(realpath "${1}")
  123. else
  124. echo ${1}
  125. fi
  126. }
  127. function control_c {
  128. if [ -f "gs_starter.run" ]; then
  129. rm "gs_starter.run"
  130. fi
  131. exit $?
  132. }
  133. function watcher {
  134. # trap keyboard interrupt (control-c)
  135. trap control_c SIGINT
  136. echo "$$ $(date) watcher runs" > "gs_starter.run"
  137. echo "Night gathers, and now my watch begins. Lets keep those activated: ${SCREEN[*]}"
  138. # start them..
  139. while [ -f "gs_starter.run" ]; do
  140. for index in ${!ACTIVATE[*]}
  141. do
  142. #start, if not running .. checks if running are in start_instance
  143. start_instance ${ACTIVATE[$index]}
  144. done
  145. sleep 4
  146. loadcfg
  147. done
  148. }
  149. function checkinstalled {
  150. if [ "$1" != "" ]; then
  151. I=$(which "$1" 2> /dev/null)
  152. if [ -f "$I" ]; then
  153. echo 1
  154. else
  155. echo 0
  156. fi
  157. fi
  158. }
  159. function main {
  160. loadcfg first
  161. if [ "$(checkinstalled "realpath")" == "1" ]; then
  162. RPQ=$(realpath "$GSDED")
  163. else
  164. RPQ=$GSDED
  165. fi
  166. FBITS=$(find_filebits $RPQ)
  167. SBITS=$(find_sysbits)
  168. #echo fbits: $FBITS
  169. #echo sbits: $SBITS
  170. #if [ $SBITS -gt $FBITS ]; then
  171. # # link to 32bit libs
  172. # s_lib="lib32/"
  173. #fi
  174. if [ $SBITS -lt $FBITS ]; then
  175. echo "Can't start 64 bit executables ($GSDED) on this system."
  176. return 0
  177. fi
  178. #lib dir for ppl
  179. s_lib="LD_LIBRARY_PATH=libs/:$LD_LIBRARY_PATH"
  180. if [ "$1" == "" ]; then
  181. echo "Usage: gs_starter.sh <startwatch|stopwatch|stopall|start <instance#>|stop <instance#>>"
  182. return 1
  183. elif [ "$1" == "startwatch" ] || [ "$1" == "startall" ]; then
  184. ACTION="watch"
  185. # check if watcher is already running
  186. if [ -f "gs_starter.run" ]; then
  187. echo "gs_starter.sh is already running. Not starting again."
  188. return 0
  189. else
  190. watcher
  191. return 1
  192. fi
  193. elif [ "$1" == "stopwatch" ]; then
  194. rm "gs_starter.run"
  195. echo "And now his watch is ended. The gs_starter watcher should be stopped now."
  196. return 1
  197. elif [ "$1" == "stopall" ]; then
  198. echo "Stopping all instances."
  199. echo "Pro-Tip: If the watcher is running, the servers will come up again. Useful to restart all instances."
  200. # find all screens with "$PREFIX"
  201. SCRPIDLIST=$(screen -ls | grep -i \.$PREFIX | cut -f1 -d\. | awk {'print $1'})
  202. for x in $SCRPIDLIST; do
  203. kill -9 $x
  204. done
  205. screen -wipe > /dev/null
  206. return 1
  207. elif [ "$1" == "start" ]; then
  208. if [ "$2" != "" ]; then
  209. NUMBER=$2
  210. echo "Starting instance $NUMBER."
  211. echo "Pro-Tip: If you want the watcher to watch this instance, edit gs_starter.cfg parameter ACTIVATE now."
  212. echo " The watcher reloads the config and will begin watching it."
  213. # find all screens with "$PREFIX$NUMBER"
  214. start_instance $NUMBER
  215. return 1
  216. else
  217. echo "Usage: gs_starter.sh start <instance#>"
  218. return 0
  219. fi
  220. elif [ "$1" == "stop" ]; then
  221. if [ "$2" != "" ]; then
  222. NUMBER=$2
  223. echo "Stopping instance $NUMBER."
  224. echo "Pro-Tip: If the watcher is running, it may come up again. So this is useful for restarting with different PARMS."
  225. # find all screens with "$PREFIX$NUMBER"
  226. SCRPID=$(screen -ls | grep -i \.$PREFIX$NUMBER | cut -f1 -d\. | awk {'print $1'})
  227. kill -9 $SCRPID
  228. screen -wipe
  229. return 1
  230. else
  231. echo "Usage: gs_starter.sh stop <instance#>"
  232. return 0
  233. fi
  234. fi
  235. }
  236. main $*
  237. # vim: tabstop=2 shiftwidth=2 softtabstop=2 autoindent:
  238. # kate: space-indent off; indent-width 2; mixedindent off;