4
0

command_stop.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #!/bin/bash
  2. # LinuxGSM command_stop.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://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 "${telnetpassword}" ] || [ "${telnetpassword}" == "NOT SET" ]; then
  95. sdtdtelnetshutdown=$(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. sdtdtelnetshutdown=$(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 "'"${telnetpassword}"'\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 ${sdtdtelnetshutdown}" | grep "Completed.")
  141. refused=$(echo -en "\n ${sdtdtelnetshutdown}" | 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 ${sdtdtelnetshutdown}" | 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 ${sdtdtelnetshutdown}" | 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 of Soulmask using telnet.
  191. fn_stop_graceful_sm() {
  192. fn_print_dots "Graceful: telnet"
  193. fn_script_log_info "Graceful: telnet"
  194. if [ "${telnetenabled}" == "false" ]; then
  195. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  196. elif [ "$(command -v expect 2> /dev/null)" ]; then
  197. # Tries to shutdown with both localhost and server IP.
  198. for telnetip in 127.0.0.1 ${ip}; do
  199. fn_print_dots "Graceful: telnet: ${telnetip}:${telnetport}"
  200. fn_script_log_info "Graceful: telnet: ${telnetip}:${telnetport}"
  201. fn_stop_graceful_sm_telnet
  202. completed=$(echo -en "\n ${smtelnetshutdown}" | grep "Completed.")
  203. refused=$(echo -en "\n ${smtelnetshutdown}" | grep "Timeout or EOF")
  204. if [ "${refused}" ]; then
  205. fn_print_error "Graceful: telnet: ${telnetip}:${telnetport} : "
  206. fn_print_fail_eol_nl
  207. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  208. elif [ "${completed}" ]; then
  209. break
  210. fi
  211. done
  212. # If telnet shutdown was successful will use telnet again to check
  213. # the connection has closed, confirming that the tmux session can now be killed.
  214. if [ "${completed}" ]; then
  215. for seconds in {1..30}; do
  216. fn_stop_graceful_sm_telnet
  217. refused=$(echo -en "\n ${smtelnetshutdown}" | grep "Timeout or EOF")
  218. if [ "${refused}" ]; then
  219. fn_print_ok "Graceful: telnet: ${telnetip}:${telnetport} : "
  220. fn_print_ok_eol_nl
  221. fn_script_log_pass "Graceful: telnet: ${telnetip}:${telnetport} : ${seconds} seconds"
  222. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  223. alert="stopped"
  224. alert.sh
  225. fi
  226. break
  227. fi
  228. fn_sleep_time_1
  229. fn_print_dots "Graceful: telnet: ${seconds}"
  230. done
  231. # If telnet shutdown fails, show it and stop
  232. else
  233. if [ "${refused}" ]; then
  234. fn_print_error "Graceful: telnet: "
  235. fn_print_fail_eol_nl
  236. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  237. else
  238. fn_print_error_nl "Graceful: telnet: Unknown error"
  239. fn_script_log_error "Graceful: telnet: Unknown error"
  240. fi
  241. echo -en "\n" | tee -a "${lgsmlog}"
  242. echo -en "Telnet output:" | tee -a "${lgsmlog}"
  243. echo -en "\n ${smtelnetshutdown}" | tee -a "${lgsmlog}"
  244. echo -en "\n\n" | tee -a "${lgsmlog}"
  245. fi
  246. else
  247. fn_print_warn "Graceful: telnet: expect not installed: "
  248. fn_print_fail_eol_nl
  249. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  250. fi
  251. }
  252. # telnet command for soulmask graceful shutdown.
  253. fn_stop_graceful_sm_telnet() {
  254. smtelnetshutdown=$(expect -c '
  255. proc abort {} {
  256. puts "Timeout or EOF\n"
  257. exit 1
  258. }
  259. spawn telnet '"${telnetip}"' '"${telnetport}"'
  260. expect {
  261. "Hello:" { send "saveworld 1\r" }
  262. default abort
  263. }
  264. expect {
  265. "the world is saved." { send "quit 1\r" }
  266. default abort
  267. }
  268. expect {
  269. "World is closing..." {}
  270. default abort
  271. }
  272. expect { eof }
  273. puts "Completed.\n"
  274. ')
  275. }
  276. # Attempts graceful shutdown by sending /save /stop.
  277. fn_stop_graceful_avorion() {
  278. fn_print_dots "Graceful: /save /stop"
  279. fn_script_log_info "Graceful: /save /stop"
  280. # Sends /save.
  281. tmux -L "${socketname}" send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1
  282. fn_sleep_time_5
  283. # Sends /quit.
  284. tmux -L "${socketname}" send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1
  285. # Waits up to 30 seconds giving the server time to shutdown gracefuly.
  286. for seconds in {1..30}; do
  287. check_status.sh
  288. if [ "${status}" == "0" ]; then
  289. fn_print_ok "Graceful: /save /stop: ${seconds}"
  290. fn_print_ok_eol_nl
  291. fn_script_log_pass "Graceful: /save /stop: OK: ${seconds} seconds"
  292. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  293. alert="stopped"
  294. alert.sh
  295. fi
  296. break
  297. fi
  298. fn_sleep_time_1
  299. fn_print_dots "Graceful: /save /stop: ${seconds}"
  300. done
  301. check_status.sh
  302. if [ "${status}" != "0" ]; then
  303. fn_print_error "Graceful: /save /stop: "
  304. fn_print_fail_eol_nl
  305. fn_script_log_error "Graceful: /save /stop: FAIL"
  306. fi
  307. }
  308. fn_stop_graceful_select() {
  309. if [ "${stopmode}" == "1" ]; then
  310. fn_stop_tmux
  311. elif [ "${stopmode}" == "2" ]; then
  312. fn_stop_graceful_ctrlc
  313. elif [ "${stopmode}" == "3" ]; then
  314. fn_stop_graceful_cmd "quit" 30
  315. elif [ "${stopmode}" == "4" ]; then
  316. fn_stop_graceful_cmd "quit" 120
  317. elif [ "${stopmode}" == "5" ]; then
  318. fn_stop_graceful_cmd "stop" 30
  319. elif [ "${stopmode}" == "6" ]; then
  320. fn_stop_graceful_cmd "q" 30
  321. elif [ "${stopmode}" == "7" ]; then
  322. fn_stop_graceful_cmd "exit" 30
  323. elif [ "${stopmode}" == "8" ]; then
  324. fn_stop_graceful_sdtd
  325. elif [ "${stopmode}" == "9" ]; then
  326. fn_stop_graceful_goldsrc
  327. elif [ "${stopmode}" == "10" ]; then
  328. fn_stop_graceful_avorion
  329. elif [ "${stopmode}" == "11" ]; then
  330. fn_stop_graceful_cmd "end" 30
  331. elif [ "${stopmode}" == "12" ]; then
  332. fn_stop_graceful_cmd "shutdown" 30
  333. elif [ "${stopmode}" == "13" ]; then
  334. fn_stop_graceful_sm
  335. fi
  336. }
  337. fn_stop_tmux() {
  338. fn_print_dots "${servername}"
  339. fn_script_log_info "tmux kill-session: ${sessionname}: ${servername}"
  340. # Kill tmux session.
  341. tmux -L "${socketname}" kill-session -t "${sessionname}" > /dev/null 2>&1
  342. fn_sleep_time_1
  343. check_status.sh
  344. if [ "${status}" == "0" ]; then
  345. fn_print_ok_nl "${servername}"
  346. fn_script_log_pass "Stopped ${servername}"
  347. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  348. alert="stopped"
  349. alert.sh
  350. fi
  351. else
  352. fn_print_fail_nl "Unable to stop ${servername}"
  353. fn_script_log_fail "Unable to stop ${servername}"
  354. fi
  355. }
  356. # Checks if the server is already stopped.
  357. fn_stop_pre_check() {
  358. if [ "${status}" == "0" ]; then
  359. fn_print_skip_nl "${servername} is already stopped"
  360. fn_script_log_info "${servername} is already stopped"
  361. else
  362. # Select graceful shutdown.
  363. fn_stop_graceful_select
  364. # Check status again, a kill tmux session if graceful shutdown failed.
  365. check_status.sh
  366. if [ "${status}" != "0" ]; then
  367. fn_stop_tmux
  368. fi
  369. fi
  370. }
  371. fn_print_dots ""
  372. check.sh
  373. # Create a stopping lockfile that only exists while the stop command is running.
  374. date '+%s' > "${lockdir:?}/${selfname}-stopping.lock"
  375. fn_print_dots "${servername}"
  376. info_game.sh
  377. fn_stop_pre_check
  378. # Remove started lockfile.
  379. rm -f "${lockdir:?}/${selfname}-started.lock"
  380. # If user ran the stop command monitor will become disabled.
  381. if [ "${firstcommandname}" == "STOP" ]; then
  382. rm -f "${lockdir:?}/${selfname}-monitoring.lock"
  383. fi
  384. # Remove stopping lockfile.
  385. rm -f "${lockdir:?}/${selfname}-stopping.lock"
  386. if [ -z "${exitbypass}" ]; then
  387. core_exit.sh
  388. fi