gs_starter.sh 5.5 KB

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