4
0

command_stop.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #!/bin/bash
  2. # LinuxGSM command_stop.sh function
  3. # Author: Daniel Gibbs
  4. # Contributors: UltimateByte
  5. # Website: https://linuxgsm.com
  6. # Description: Stops the server.
  7. fn_commandname(){
  8. commandname="STOP"
  9. commandaction="Stopping"
  10. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  11. }
  12. fn_commandname
  13. # Attempts graceful shutdown by sending 'CTRL+c'.
  14. fn_stop_graceful_ctrlc(){
  15. fn_print_dots "Graceful: CTRL+c"
  16. fn_script_log_info "Graceful: CTRL+c"
  17. # Sends quit.
  18. tmux send-keys -t "${sessionname}" C-c > /dev/null 2>&1
  19. # Waits up to 30 seconds giving the server time to shutdown gracefuly.
  20. for seconds in {1..30}; do
  21. check_status.sh
  22. if [ "${status}" == "0" ]; then
  23. fn_print_ok "Graceful: CTRL+c: ${seconds}: "
  24. fn_print_ok_eol_nl
  25. fn_script_log_pass "Graceful: CTRL+c: OK: ${seconds} seconds"
  26. break
  27. fi
  28. sleep 1
  29. fn_print_dots "Graceful: CTRL+c: ${seconds}"
  30. done
  31. check_status.sh
  32. if [ "${status}" != "0" ]; then
  33. fn_print_error "Graceful: CTRL+c: "
  34. fn_print_fail_eol_nl
  35. fn_script_log_error "Graceful: CTRL+c: FAIL"
  36. fi
  37. }
  38. # Attempts graceful shutdown by sending a specified command.
  39. # Usage: fn_stop_graceful_cmd "console_command" "timeout_in_seconds"
  40. # e.g.: fn_stop_graceful_cmd "quit" "30"
  41. fn_stop_graceful_cmd(){
  42. fn_print_dots "Graceful: sending \"${1}\""
  43. fn_script_log_info "Graceful: sending \"${1}\""
  44. # Sends specific stop command.
  45. tmux send -t "${sessionname}" "${1}" ENTER > /dev/null 2>&1
  46. # Waits up to ${seconds} seconds giving the server time to shutdown gracefully.
  47. for ((seconds=1; seconds<=${2}; seconds++)); do
  48. check_status.sh
  49. if [ "${status}" == "0" ]; then
  50. fn_print_ok "Graceful: sending \"${1}\": ${seconds}: "
  51. fn_print_ok_eol_nl
  52. fn_script_log_pass "Graceful: sending \"${1}\": OK: ${seconds} seconds"
  53. break
  54. fi
  55. sleep 1
  56. fn_print_dots "Graceful: sending \"${1}\": ${seconds}"
  57. done
  58. check_status.sh
  59. if [ "${status}" != "0" ]; then
  60. fn_print_error "Graceful: sending \"${1}\": "
  61. fn_print_fail_eol_nl
  62. fn_script_log_error "Graceful: sending \"${1}\": FAIL"
  63. fi
  64. }
  65. # Attempts graceful shutdown of goldsrc using rcon 'quit' command.
  66. # There is only a 3 second delay before a forced a tmux shutdown
  67. # as GoldSrc servers 'quit' command does a restart rather than shutdown.
  68. fn_stop_graceful_goldsrc(){
  69. fn_print_dots "Graceful: sending \"quit\""
  70. fn_script_log_info "Graceful: sending \"quit\""
  71. # sends quit
  72. tmux send -t "${sessionname}" quit ENTER > /dev/null 2>&1
  73. # Waits 3 seconds as goldsrc servers restart with the quit command.
  74. for seconds in {1..3}; do
  75. sleep 1
  76. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  77. done
  78. fn_print_ok "Graceful: sending \"quit\": ${seconds}: "
  79. fn_print_ok_eol_nl
  80. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  81. }
  82. # telnet command for sdtd graceful shutdown.
  83. fn_stop_graceful_sdtd_telnet(){
  84. if [ -z "${telnetpass}" ]||[ "${telnetpass}" == "NOT SET" ]; then
  85. sdtd_telnet_shutdown=$( expect -c '
  86. proc abort {} {
  87. puts "Timeout or EOF\n"
  88. exit 1
  89. }
  90. spawn telnet '"${telnetip}"' '"${telnetport}"'
  91. expect {
  92. "session." { send "shutdown\r" }
  93. default abort
  94. }
  95. expect { eof }
  96. puts "Completed.\n"
  97. ')
  98. else
  99. sdtd_telnet_shutdown=$( expect -c '
  100. proc abort {} {
  101. puts "Timeout or EOF\n"
  102. exit 1
  103. }
  104. spawn telnet '"${telnetip}"' '"${telnetport}"'
  105. expect {
  106. "password:" { send "'"${telnetpass}"'\r" }
  107. default abort
  108. }
  109. expect {
  110. "session." { send "shutdown\r" }
  111. default abort
  112. }
  113. expect { eof }
  114. puts "Completed.\n"
  115. ')
  116. fi
  117. }
  118. # Attempts graceful shutdown of 7 Days To Die using telnet.
  119. fn_stop_graceful_sdtd(){
  120. fn_print_dots "Graceful: telnet"
  121. fn_script_log_info "Graceful: telnet"
  122. if [ "${telnetenabled}" == "false" ]; then
  123. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  124. elif [ "$(command -v expect 2>/dev/null)" ]; then
  125. # Tries to shutdown with both localhost and server IP.
  126. for telnetip in 127.0.0.1 ${ip}; do
  127. fn_print_dots "Graceful: telnet: ${telnetip}:${telnetport}"
  128. fn_script_log_info "Graceful: telnet: ${telnetip}:${telnetport}"
  129. fn_stop_graceful_sdtd_telnet
  130. completed=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Completed.")
  131. refused=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Timeout or EOF")
  132. if [ "${refused}" ]; then
  133. fn_print_error "Graceful: telnet: ${telnetip}:${telnetport} : "
  134. fn_print_fail_eol_nl
  135. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  136. elif [ "${completed}" ]; then
  137. break
  138. fi
  139. done
  140. # If telnet shutdown was successful will use telnet again to check
  141. # the connection has closed, confirming that the tmux session can now be killed.
  142. if [ "${completed}" ]; then
  143. for seconds in {1..30}; do
  144. fn_stop_graceful_sdtd_telnet
  145. refused=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Timeout or EOF")
  146. if [ "${refused}" ]; then
  147. fn_print_ok "Graceful: telnet: ${telnetip}:${telnetport} : "
  148. fn_print_ok_eol_nl
  149. fn_script_log_pass "Graceful: telnet: ${telnetip}:${telnetport} : ${seconds} seconds"
  150. break
  151. fi
  152. sleep 1
  153. fn_print_dots "Graceful: telnet: ${seconds}"
  154. done
  155. # If telnet shutdown fails tmux shutdown will be used, this risks loss of world save.
  156. else
  157. if [ "${refused}" ]; then
  158. fn_print_error "Graceful: telnet: "
  159. fn_print_fail_eol_nl
  160. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  161. else
  162. fn_print_error_nl "Graceful: telnet: Unknown error"
  163. fn_script_log_error "Graceful: telnet: Unknown error"
  164. fi
  165. echo -en "\n" | tee -a "${lgsmlog}"
  166. echo -en "Telnet output:" | tee -a "${lgsmlog}"
  167. echo -en "\n ${sdtd_telnet_shutdown}" | tee -a "${lgsmlog}"
  168. echo -en "\n\n" | tee -a "${lgsmlog}"
  169. fi
  170. else
  171. fn_print_warn "Graceful: telnet: expect not installed: "
  172. fn_print_fail_eol_nl
  173. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  174. fi
  175. }
  176. # Attempts graceful shutdown by sending /save /stop.
  177. fn_stop_graceful_avorion(){
  178. fn_print_dots "Graceful: /save /stop"
  179. fn_script_log_info "Graceful: /save /stop"
  180. # Sends /save.
  181. tmux send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1
  182. sleep 5
  183. # Sends /quit.
  184. tmux send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1
  185. # Waits up to 30 seconds giving the server time to shutdown gracefuly.
  186. for seconds in {1..30}; do
  187. check_status.sh
  188. if [ "${status}" == "0" ]; then
  189. fn_print_ok "Graceful: /save /stop: ${seconds}: "
  190. fn_print_ok_eol_nl
  191. fn_script_log_pass "Graceful: /save /stop: OK: ${seconds} seconds"
  192. break
  193. fi
  194. sleep 1
  195. fn_print_dots "Graceful: /save /stop: ${seconds}"
  196. done
  197. check_status.sh
  198. if [ "${status}" != "0" ]; then
  199. fn_print_error "Graceful: /save /stop: "
  200. fn_print_fail_eol_nl
  201. fn_script_log_error "Graceful: /save /stop: FAIL"
  202. fi
  203. }
  204. fn_stop_graceful_select(){
  205. if [ "${stopmode}" == "1" ]; then
  206. fn_stop_tmux
  207. elif [ "${stopmode}" == "2" ]; then
  208. fn_stop_graceful_ctrlc
  209. elif [ "${stopmode}" == "3" ]; then
  210. fn_stop_graceful_cmd "quit" 30
  211. elif [ "${stopmode}" == "4" ]; then
  212. fn_stop_graceful_cmd "quit" 120
  213. elif [ "${stopmode}" == "5" ]; then
  214. fn_stop_graceful_cmd "stop" 30
  215. elif [ "${stopmode}" == "6" ]; then
  216. fn_stop_graceful_cmd "q" 30
  217. elif [ "${stopmode}" == "7" ]; then
  218. fn_stop_graceful_cmd "exit" 30
  219. elif [ "${stopmode}" == "8" ]; then
  220. fn_stop_graceful_sdtd
  221. elif [ "${stopmode}" == "9" ]; then
  222. fn_stop_graceful_goldsrc
  223. elif [ "${stopmode}" == "10" ]; then
  224. fn_stop_graceful_avorion
  225. fi
  226. }
  227. fn_stop_tmux(){
  228. fn_print_dots "${servername}"
  229. fn_script_log_info "tmux kill-session: ${sessionname}: ${servername}"
  230. # Kill tmux session.
  231. tmux kill-session -t "${sessionname}" > /dev/null 2>&1
  232. sleep 0.5
  233. check_status.sh
  234. if [ "${status}" == "0" ]; then
  235. fn_print_ok_nl "${servername}"
  236. fn_script_log_pass "Stopped ${servername}"
  237. else
  238. fn_print_fail_nl "Unable to stop ${servername}"
  239. fn_script_log_fatal "Unable to stop ${servername}"
  240. fi
  241. }
  242. # Checks if the server is already stopped.
  243. fn_stop_pre_check(){
  244. if [ "${status}" == "0" ]; then
  245. fn_print_info_nl "${servername} is already stopped"
  246. fn_script_log_error "${servername} is already stopped"
  247. else
  248. # Select graceful shutdown.
  249. fn_stop_graceful_select
  250. fi
  251. # Check status again, a kill tmux session if graceful shutdown failed.
  252. check_status.sh
  253. if [ "${status}" != "0" ]; then
  254. fn_stop_tmux
  255. fi
  256. }
  257. check.sh
  258. fn_print_dots "${servername}"
  259. info_config.sh
  260. fn_stop_pre_check
  261. # Remove lockfile.
  262. if [ -f "${lockdir}/${selfname}.lock" ]; then
  263. rm -f "${lockdir:?}/${selfname}.lock"
  264. fi
  265. if [ -z "${exitbypass}" ]; then
  266. core_exit.sh
  267. fi