4
0

command_stop.sh 8.4 KB

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