command_stop.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/bin/bash
  2. # LGSM command_stop.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://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. fn_print_dots "Graceful: telnet"
  77. fn_scriptlog "Graceful: telnet"
  78. sleep 1
  79. if [ "${telnetenabled}" == "false" ]; then
  80. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  81. elif [ "$(command -v expect)" ]||[ "$(which expect >/dev/null 2>&1)" ]; then
  82. # Tries to shutdown with both localhost and server IP.
  83. for telnetip in 127.0.0.1 ${ip}; do
  84. fn_print_dots "Graceful: telnet: ${telnetip}"
  85. fn_scriptlog "Graceful: telnet: ${telnetip}"
  86. sleep 1
  87. fn_stop_telnet_sdtd
  88. completed=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Completed.")
  89. refused=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Timeout or EOF")
  90. if [ -n "${refused}" ]; then
  91. fn_print_warn "Graceful: telnet: ${telnetip}: "
  92. fn_print_fail_eol_nl
  93. fn_scriptlog "Graceful: telnet: ${telnetip}: FAIL"
  94. sleep 1
  95. elif [ -n "${completed}" ]; then
  96. break
  97. fi
  98. done
  99. # If telnet was successful will use telnet again to check the connection has closed
  100. # This confirms that the tmux session can now be killed.
  101. if [ -n "${completed}" ]; then
  102. for seconds in {1..30}; do
  103. fn_stop_telnet_sdtd
  104. refused=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Timeout or EOF")
  105. if [ -n "${refused}" ]; then
  106. fn_print_ok "Graceful: telnet: ${telnetip}: "
  107. fn_print_ok_eol_nl
  108. fn_scriptlog "Graceful: telnet: ${telnetip}: ${seconds} seconds"
  109. break
  110. fi
  111. sleep 1
  112. fn_print_dots "Graceful: rcon quit: ${seconds}"
  113. done
  114. # If telnet failed will go straight to tmux shutdown.
  115. # If cannot shutdown correctly world save may be lost
  116. else
  117. if [ -n "${refused}" ]; then
  118. fn_print_fail "Graceful: telnet: "
  119. fn_print_fail_eol_nl
  120. fn_scriptlog "Graceful: telnet: ${telnetip}: FAIL"
  121. else
  122. fn_print_fail_nl "Graceful: telnet: Unknown error"
  123. fn_scriptlog "Graceful: telnet: Unknown error"
  124. fi
  125. echo -en "\n" | tee -a "${scriptlog}"
  126. echo -en "Telnet output:" | tee -a "${scriptlog}"
  127. echo -en "\n ${sdtd_telnet_shutdown}" | tee -a "${scriptlog}"
  128. echo -en "\n\n" | tee -a "${scriptlog}"
  129. fi
  130. else
  131. fn_print_dots "Graceful: telnet: "
  132. fn_scriptlog "Graceful: telnet: "
  133. fn_print_fail "Graceful: telnet: expect not installed: "
  134. fn_print_fail_eol_nl
  135. fn_scriptlog "Graceful: telnet: expect not installed: FAIL"
  136. fi
  137. sleep 1
  138. fn_stop_tmux
  139. }
  140. fn_stop_graceful_select(){
  141. if [ "${gamename}" == "7 Days To Die" ]; then
  142. fn_stop_graceful_sdtd
  143. elif [ "${engine}" == "source" ]; then
  144. fn_stop_graceful_source
  145. elif [ "${engine}" == "goldsource" ]; then
  146. fn_stop_graceful_goldsource
  147. else
  148. fn_stop_tmux
  149. fi
  150. }
  151. fn_stop_teamspeak3(){
  152. fn_print_dots "${servername}"
  153. fn_scriptlog "${servername}"
  154. sleep 1
  155. ${filesdir}/ts3server_startscript.sh stop > /dev/null 2>&1
  156. # Remove lock file
  157. rm -f "${rootdir}/${lockselfname}"
  158. fn_print_ok_nl "${servername}"
  159. fn_scriptlog "Stopped ${servername}"
  160. }
  161. fn_stop_tmux(){
  162. fn_print_dots "${servername}"
  163. fn_scriptlog "tmux kill-session: ${servername}"
  164. sleep 1
  165. # Kill tmux session
  166. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  167. sleep 0.5
  168. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  169. if [ "${pid}" == "0" ]; then
  170. # Remove lock file
  171. rm -f "${rootdir}/${lockselfname}"
  172. fn_print_ok_nl "${servername}"
  173. fn_scriptlog "Stopped ${servername}"
  174. else
  175. fn_print_fail_nl "Unable to stop${servername}"
  176. fn_scriptlog "Unable to stop${servername}"
  177. fi
  178. }
  179. # checks if the server is already stopped before trying to stop.
  180. fn_stop_pre_check(){
  181. if [ "${gamename}" == "Teamspeak 3" ]; then
  182. info_ts3status.sh
  183. if [ "${ts3status}" = "No server running (ts3server.pid is missing)" ]; then
  184. fn_print_ok_nl "${servername} is already stopped"
  185. fn_scriptlog "${servername} is already stopped"
  186. else
  187. fn_stop_teamspeak3
  188. fi
  189. else
  190. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  191. if [ "${pid}" == "0" ]; then
  192. fn_print_ok_nl "${servername} is already stopped"
  193. fn_scriptlog "${servername} is already stopped"
  194. else
  195. fn_stop_graceful_select
  196. fi
  197. fi
  198. }
  199. check.sh
  200. info_config.sh
  201. fn_print_dots "${servername}"
  202. fn_scriptlog "${servername}"
  203. sleep 1
  204. fn_stop_pre_check