gs_starter.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env bash
  2. #gs_starter.sh - starting and managing screens. 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. # bash is needed!!
  20. #
  21. # Paul-Dieter Klumpp, 2012-11-14
  22. # what's the q2 server binary?
  23. Q2DED="q2proded"
  24. # when no specific start parameter has been given, start these instances:
  25. ACTIVATE=(1 5)
  26. # put common settings here:
  27. p_base="+set basedir . +fs_restart +exec q2proded.cfg"
  28. p_action="+set game action +set gamedir action"
  29. # put server parameters here:
  30. PARMS[1]="$p_base $p_action +set net_port 27910 +exec aq2_1.cfg"
  31. PARMS[2]="$p_base $p_action +set net_port 27911 +exec aq2_2.cfg"
  32. PARMS[3]="$p_base $p_action +set net_port 27912 +exec aq2_3.cfg"
  33. PARMS[4]="$p_base $p_action +set net_port 27913 +exec aq2_4.cfg"
  34. PARMS[5]="$p_base $p_action +set net_port 27911 +exec aq2_5.cfg"
  35. PARMS[6]=""
  36. PARMS[7]=""
  37. PARMS[8]=""
  38. PARMS[9]=""
  39. PARMS[10]=""
  40. PARMS[11]=""
  41. PARMS[12]=""
  42. PARMS[13]=""
  43. PARMS[14]=""
  44. PARMS[15]=""
  45. PARMS[16]=""
  46. PARMS[17]=""
  47. PARMS[18]=""
  48. PARMS[19]=""
  49. PARMS[20]=""
  50. # you can define more, if you really need....
  51. ##### now, hands away... #####
  52. for index in ${!ACTIVATE[*]}; do
  53. SCREEN[${ACTIVATE[$index]}]="${Q2DED}_${ACTIVATE[$index]}"
  54. done
  55. echo "Using screen names: ${SCREEN[*]}"
  56. s_lib=""
  57. function start_instance() {
  58. PARMS=${PARMS[$1]}
  59. if [ "$PARMS" != "" ]; then
  60. # if it still runs, don't start again
  61. SCREEN=${SCREEN[$1]}
  62. if [ "$SCREEN" != "" ]; then
  63. printf "."
  64. fi
  65. running=$(screen -list | grep -vi "dead" | grep -i "$SCREEN" | awk {'print $1'} | wc -l)
  66. if [ $running -eq 0 ]; then
  67. #start it daemonized via screen
  68. shellscript="$Q2DED-$1.sh"
  69. cat > $shellscript <<here-doc
  70. #!/usr/bin/env bash
  71. while true; do
  72. echo
  73. echo "Starting '${Q2DED} ${PARMS}' $s_lib"
  74. echo
  75. LD_LIBRARY_PATH=${s_lib} ./${Q2DED} ${PARMS}
  76. echo
  77. echo "----sleep---- ctrl+c to hit the break :-)"
  78. echo
  79. sleep 3
  80. done
  81. here-doc
  82. chmod u+x $shellscript
  83. echo "Starting '${Q2DED} ${PARMS}' $s_lib"
  84. screen -dm -S "${SCREEN}" "./$shellscript"
  85. echo "on screen: ${SCREEN[$1]}"
  86. fi
  87. fi
  88. }
  89. function find_filebits {
  90. Q64=$(file "${1}" | grep -i "x86-64" | wc -l)
  91. if [ "${Q64}" == "1" ]; then
  92. echo 64
  93. return 1
  94. fi
  95. Q32=$(file "${1}" | grep -i "32-bit" | wc -l)
  96. if [ "${Q32}" == "1" ]; then
  97. echo 32
  98. return 1
  99. fi
  100. # fallback and assume system bits
  101. sb=find_sysbits
  102. echo $sb
  103. return 1
  104. }
  105. function find_sysbits {
  106. S64=$(uname -a | grep -i "x86_64" | wc -l)
  107. if [ "${S64}" == "1" ]; then
  108. echo 64
  109. else
  110. echo 32
  111. fi
  112. }
  113. function f_realpath() {
  114. RPBIN=`which realpath`
  115. if [ -x "${RPBIN}" ]; then
  116. echo $(realpath "${1}")
  117. else
  118. echo ${1}
  119. fi
  120. }
  121. function main {
  122. RPQ=$(f_realpath "$Q2DED")
  123. FBITS=$(find_filebits $RPQ)
  124. SBITS=$(find_sysbits)
  125. #echo fbits: $FBITS
  126. #echo sbits: $SBITS
  127. if [ $SBITS -gt $FBITS ]; then
  128. # link to 32bit libs
  129. s_lib="lib32/"
  130. fi
  131. if [ $SBITS -lt $FBITS ]; then
  132. echo "Can't start 64 bit executables ($Q2DED) on this system."
  133. return 0
  134. fi
  135. echo "Checking if instances ${ACTIVATE[*]} need to run."
  136. # start them..
  137. while true; do
  138. for index in ${!ACTIVATE[*]}
  139. do
  140. #start, if not running .. checks if running are in start_instance_loop
  141. start_instance ${ACTIVATE[$index]}
  142. done
  143. sleep 3
  144. done
  145. }
  146. main