command_stop.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/bin/bash
  2. # LGSM command_stop.sh function
  3. # Author: Daniel Gibbs
  4. # Website: http://gameservermanagers.com
  5. lgsm_version="271215"
  6. # Description: Stops the server.
  7. local modulename="Stopping"
  8. function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  9. # Attempts Graceful of source using rcon 'quit' command.
  10. fn_stop_graceful_source(){
  11. fn_print_dots "Graceful: rcon quit"
  12. fn_scriptlog "Graceful: rcon quit"
  13. # sends quit
  14. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  15. # waits up to 30 seconds giving the server time to shutdown gracefuly
  16. for seconds in {1..30}; do
  17. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  18. if [ "${pid}" == "0" ]; then
  19. fn_print_ok "Graceful: rcon quit: ${seconds}: "
  20. fn_print_ok_eol_nl
  21. fn_scriptlog "Graceful: rcon quit: OK: ${seconds} seconds"
  22. break
  23. fi
  24. sleep 1
  25. fn_print_dots "Graceful: rcon quit: ${seconds}"
  26. done
  27. if [ "${pid}" != "0" ]; then
  28. fn_print_fail "Graceful: rcon quit"
  29. fn_print_fail_eol_nl
  30. fn_scriptlog "Graceful: rcon quit: FAIL"
  31. fn_stop_tmux
  32. fi
  33. sleep 1
  34. }
  35. # Attempts Graceful of goldsource using rcon 'quit' command.
  36. # Goldsource 'quit' command restarts rather than shutsdown
  37. # this function will only wait 3 seconds then force a tmux shutdown.
  38. # preventing the server from coming back online.
  39. fn_stop_graceful_goldsource(){
  40. fn_print_dots "Graceful: rcon quit"
  41. fn_scriptlog "Graceful: rcon quit"
  42. # sends quit
  43. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  44. # waits 3 seconds as goldsource servers restart with the quit command
  45. for seconds in {1..3}; do
  46. sleep 1
  47. fn_print_dots "Graceful: rcon quit: ${seconds}"
  48. done
  49. fn_print_ok "Graceful: rcon quit: ${seconds}: "
  50. fn_print_ok_eol_nl
  51. fn_scriptlog "Graceful: rcon quit: OK: ${seconds} seconds"
  52. sleep 1
  53. fn_stop_tmux
  54. }
  55. # Attempts Graceful of 7 Days To Die using telnet.
  56. fn_stop_telnet_sdtd(){
  57. sdtd_telnet_shutdown=$( expect -c '
  58. proc abort {} {
  59. puts "Timeout or EOF\n"
  60. exit 1
  61. }
  62. spawn telnet '"${telnetip}"' '"${telnetport}"'
  63. expect {
  64. "password:" { send "'"${telnetpass}"'\r" }
  65. default abort
  66. }
  67. expect {
  68. "session." { send "shutdown\r" }
  69. default abort
  70. }
  71. expect { eof }
  72. puts "Completed.\n"
  73. ')
  74. }
  75. fn_stop_graceful_sdtd(){
  76. # Gets server IP.
  77. info_config.sh
  78. fn_print_dots "Graceful: telnet"
  79. fn_scriptlog "Graceful: telnet"
  80. sleep 1
  81. if [ "${telnetenabled}" == "false" ]; then
  82. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  83. elif [ "$(command -v expect)" ]||[ "$(which expect >/dev/null 2>&1)" ]; then
  84. # Tries to shutdown with both localhost and server IP.
  85. for telnetip in 127.0.0.1 ${ip}; do
  86. fn_print_dots "Graceful: telnet: ${telnetip}"
  87. fn_scriptlog "Graceful: telnet: ${telnetip}"
  88. sleep 1
  89. fn_stop_telnet_sdtd
  90. completed=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Completed.")
  91. refused=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Timeout or EOF")
  92. if [ -n "${refused}" ]; then
  93. fn_print_warn "Graceful: telnet: ${telnetip}: "
  94. fn_print_fail_eol_nl
  95. fn_scriptlog "Graceful: telnet: ${telnetip}: FAIL"
  96. sleep 1
  97. elif [ -n "${completed}" ]; then
  98. break
  99. fi
  100. done
  101. # If telnet was successful will use telnet again to check the connection has closed
  102. # This confirms that the tmux session can now be killed.
  103. if [ -n "${completed}" ]; then
  104. for seconds in {1..30}; do
  105. fn_stop_telnet_sdtd
  106. refused=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Timeout or EOF")
  107. if [ -n "${refused}" ]; then
  108. fn_print_ok "Graceful: telnet: ${telnetip}: "
  109. fn_print_ok_eol_nl
  110. fn_scriptlog "Graceful: telnet: ${telnetip}: ${seconds} seconds"
  111. break
  112. fi
  113. sleep 1
  114. fn_print_dots "Graceful: rcon quit: ${seconds}"
  115. done
  116. # If telnet failed will go straight to tmux shutdown.
  117. # If cannot shutdown correctly world save may be lost
  118. else
  119. if [ -n "${refused}" ]; then
  120. fn_print_fail "Graceful: telnet: "
  121. fn_print_fail_eol_nl
  122. fn_scriptlog "Graceful: telnet: ${telnetip}: FAIL"
  123. else
  124. fn_print_fail_nl "Graceful: telnet: Unknown error"
  125. fn_scriptlog "Graceful: telnet: Unknown error"
  126. fi
  127. echo -en "\n\n" | tee -a "${scriptlog}"
  128. echo -en "Telnet output:" | tee -a "${scriptlog}"
  129. echo -en "\n ${sdtd_telnet_shutdown}" | tee -a "${scriptlog}"
  130. echo -en "\n\n" | tee -a "${scriptlog}"
  131. fi
  132. else
  133. fn_print_dots "Graceful: telnet: "
  134. fn_scriptlog "Graceful: telnet: "
  135. fn_print_fail "Graceful: telnet: expect not installed: "
  136. fn_print_fail_eol_nl
  137. fn_scriptlog "Graceful: telnet: expect not installed: FAIL"
  138. fi
  139. sleep 1
  140. fn_stop_tmux
  141. }
  142. fn_stop_graceful_select(){
  143. if [ "${gamename}" == "7 Days To Die" ]; then
  144. fn_stop_graceful_sdtd
  145. elif [ "${engine}" == "source" ]; then
  146. fn_stop_graceful_source
  147. elif [ "${engine}" == "goldsource" ]; then
  148. fn_stop_graceful_goldsource
  149. else
  150. fn_stop_tmux
  151. fi
  152. }
  153. fn_stop_teamspeak3(){
  154. fn_print_dots "${servername}"
  155. fn_scriptlog "${servername}"
  156. sleep 1
  157. ${filesdir}/ts3server_startscript.sh stop > /dev/null 2>&1
  158. # Remove lock file
  159. rm -f "${rootdir}/${lockselfname}"
  160. fn_print_ok_nl "${servername}"
  161. fn_scriptlog "Stopped ${servername}"
  162. }
  163. fn_stop_tmux(){
  164. fn_print_dots "${servername}"
  165. fn_scriptlog "tmux kill-session: ${servername}"
  166. sleep 1
  167. # Kill tmux session
  168. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  169. sleep 0.5
  170. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  171. if [ "${pid}" == "0" ]; then
  172. # Remove lock file
  173. rm -f "${rootdir}/${lockselfname}"
  174. fn_print_ok_nl "${servername}"
  175. fn_scriptlog "Stopped ${servername}"
  176. else
  177. fn_print_fail_nl "Unable to stop${servername}"
  178. fn_scriptlog "Unable to stop${servername}"
  179. fi
  180. }
  181. # checks if the server is already stopped before trying to stop.
  182. fn_stop_pre_check(){
  183. if [ "${gamename}" == "Teamspeak 3" ]; then
  184. info_ts3status.sh
  185. if [ "${ts3status}" = "No server running (ts3server.pid is missing)" ]; then
  186. fn_print_ok_nl "${servername} is already stopped"
  187. fn_scriptlog "${servername} is already stopped"
  188. else
  189. fn_stop_teamspeak3
  190. fi
  191. else
  192. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  193. if [ "${pid}" == "0" ]; then
  194. fn_print_ok_nl "${servername} is already stopped"
  195. fn_scriptlog "${servername} is already stopped"
  196. else
  197. fn_stop_graceful_select
  198. fi
  199. fi
  200. }
  201. check.sh
  202. fn_print_dots "${servername}"
  203. fn_scriptlog "${servername}"
  204. sleep 1
  205. fn_stop_pre_check