4
0

command_stop.sh 9.8 KB

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