command_stop.sh 8.5 KB

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