command_stop.sh 6.2 KB

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