command_stop.sh 11 KB

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