command_stop.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. # Only stop the server if there are no players online.
  12. # An explicit stop or restart always respects players when the feature is on.
  13. # Other commands that stop the server as a side effect (e.g. update) only respect
  14. # players when stoponlyifnoplayersallcommands is on.
  15. fn_stop_players_online() {
  16. if [ "${firstcommandname}" == "STOP" ] || [ "${firstcommandname}" == "RESTART" ] || [ "${stoponlyifnoplayersallcommands}" == "on" ]; then
  17. check_players_online.sh
  18. if [ -n "${playersonline}" ]; then
  19. fn_print_info_nl "${playersonline} players are on the server: stop prevented"
  20. fn_script_log_info "${playersonline} players are on the server: stop prevented"
  21. echo "${playersonline}" > "${lockdir:?}/${selfname}-player-numbers.lock"
  22. core_exit.sh
  23. fi
  24. fi
  25. }
  26. # Attempts graceful shutdown by sending 'CTRL+c'.
  27. fn_stop_graceful_ctrlc() {
  28. fn_print_dots "Graceful: CTRL+c"
  29. fn_script_log_info "Graceful: CTRL+c"
  30. # Sends CTRL+c.
  31. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" C-c > /dev/null 2>&1
  32. # Waits up to 30 seconds giving the server time to shutdown gracefully.
  33. for seconds in {1..30}; do
  34. check_status.sh
  35. if [ "${status}" == "0" ]; then
  36. fn_print_ok "Graceful: CTRL+c: ${seconds}"
  37. fn_print_ok_eol_nl
  38. fn_script_log_pass "Graceful: CTRL+c: OK: ${seconds} seconds"
  39. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  40. alert="stopped"
  41. alert.sh
  42. fi
  43. break
  44. fi
  45. fn_sleep_time_1
  46. fn_print_dots "Graceful: CTRL+c: ${seconds}"
  47. done
  48. check_status.sh
  49. if [ "${status}" != "0" ]; then
  50. fn_print_error "Graceful: CTRL+c: "
  51. fn_print_fail_eol_nl
  52. fn_script_log_error "Graceful: CTRL+c: FAIL"
  53. fi
  54. }
  55. # Attempts graceful shutdown by sending a specified command.
  56. # Usage: fn_stop_graceful_cmd "console_command" "timeout_in_seconds"
  57. # e.g.: fn_stop_graceful_cmd "quit" "30"
  58. fn_stop_graceful_cmd() {
  59. fn_print_dots "Graceful: sending \"${1}\""
  60. fn_script_log_info "Graceful: sending \"${1}\""
  61. # Sends specific stop command.
  62. TERM=screen tmux -L "${socketname}" send -t "${sessionname}" ENTER "${1}" ENTER > /dev/null 2>&1
  63. # Waits up to ${seconds} seconds giving the server time to shutdown gracefully.
  64. for ((seconds = 1; seconds <= ${2}; seconds++)); do
  65. check_status.sh
  66. if [ "${status}" == "0" ]; then
  67. fn_print_ok "Graceful: sending \"${1}\": ${seconds}"
  68. fn_print_ok_eol_nl
  69. fn_script_log_pass "Graceful: sending \"${1}\": OK: ${seconds} seconds"
  70. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  71. alert="stopped"
  72. alert.sh
  73. fi
  74. break
  75. fi
  76. fn_sleep_time_1
  77. fn_print_dots "Graceful: sending \"${1}\": ${seconds}"
  78. done
  79. check_status.sh
  80. if [ "${status}" != "0" ]; then
  81. fn_print_error "Graceful: sending \"${1}\": "
  82. fn_print_fail_eol_nl
  83. fn_script_log_error "Graceful: sending \"${1}\": FAIL"
  84. fi
  85. }
  86. # Attempts graceful shutdown of goldsrc using rcon 'quit' command.
  87. # There is only a 3 second delay before a forced a tmux shutdown
  88. # as GoldSrc servers 'quit' command does a restart rather than shutdown.
  89. fn_stop_graceful_goldsrc() {
  90. fn_print_dots "Graceful: sending \"quit\""
  91. fn_script_log_info "Graceful: sending \"quit\""
  92. # sends quit
  93. TERM=screen tmux -L "${socketname}" send -t "${sessionname}" quit ENTER > /dev/null 2>&1
  94. # Waits 3 seconds as goldsrc servers restart with the quit command.
  95. for seconds in {1..3}; do
  96. fn_sleep_time_1
  97. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  98. done
  99. fn_print_ok "Graceful: sending \"quit\": ${seconds}"
  100. fn_print_ok_eol_nl
  101. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  102. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  103. alert="stopped"
  104. alert.sh
  105. fi
  106. }
  107. # telnet command for sdtd graceful shutdown.
  108. fn_stop_graceful_sdtd_telnet() {
  109. if [ -z "${telnetpassword}" ] || [ "${telnetpassword}" == "NOT SET" ]; then
  110. sdtdtelnetshutdown=$(expect -c '
  111. proc abort {} {
  112. puts "Timeout or EOF\n"
  113. exit 1
  114. }
  115. spawn telnet '"${telnetip}"' '"${telnetport}"'
  116. expect {
  117. "session." { send "shutdown\r" }
  118. default abort
  119. }
  120. expect { eof }
  121. puts "Completed.\n"
  122. ')
  123. else
  124. sdtdtelnetshutdown=$(expect -c '
  125. proc abort {} {
  126. puts "Timeout or EOF\n"
  127. exit 1
  128. }
  129. spawn telnet '"${telnetip}"' '"${telnetport}"'
  130. expect {
  131. "password:" { send "'"${telnetpassword}"'\r" }
  132. default abort
  133. }
  134. expect {
  135. "session." { send "shutdown\r" }
  136. default abort
  137. }
  138. expect { eof }
  139. puts "Completed.\n"
  140. ')
  141. fi
  142. }
  143. # Attempts graceful shutdown of 7 Days To Die using telnet.
  144. fn_stop_graceful_sdtd() {
  145. fn_print_dots "Graceful: telnet"
  146. fn_script_log_info "Graceful: telnet"
  147. if [ "${telnetenabled}" == "false" ]; then
  148. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  149. elif [ "$(command -v expect 2> /dev/null)" ]; then
  150. # Tries to shutdown with both localhost and server IP.
  151. for telnetip in 127.0.0.1 ${ip}; do
  152. fn_print_dots "Graceful: telnet: ${telnetip}:${telnetport}"
  153. fn_script_log_info "Graceful: telnet: ${telnetip}:${telnetport}"
  154. fn_stop_graceful_sdtd_telnet
  155. completed=$(echo -en "\n ${sdtdtelnetshutdown}" | grep "Completed.")
  156. refused=$(echo -en "\n ${sdtdtelnetshutdown}" | grep "Timeout or EOF")
  157. if [ "${refused}" ]; then
  158. fn_print_error "Graceful: telnet: ${telnetip}:${telnetport} : "
  159. fn_print_fail_eol_nl
  160. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  161. elif [ "${completed}" ]; then
  162. break
  163. fi
  164. done
  165. # If telnet shutdown was successful will use telnet again to check
  166. # the connection has closed, confirming that the tmux session can now be killed.
  167. if [ "${completed}" ]; then
  168. for seconds in {1..30}; do
  169. fn_stop_graceful_sdtd_telnet
  170. refused=$(echo -en "\n ${sdtdtelnetshutdown}" | grep "Timeout or EOF")
  171. if [ "${refused}" ]; then
  172. fn_print_ok "Graceful: telnet: ${telnetip}:${telnetport} : "
  173. fn_print_ok_eol_nl
  174. fn_script_log_pass "Graceful: telnet: ${telnetip}:${telnetport} : ${seconds} seconds"
  175. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  176. alert="stopped"
  177. alert.sh
  178. fi
  179. break
  180. fi
  181. fn_sleep_time_1
  182. fn_print_dots "Graceful: telnet: ${seconds}"
  183. done
  184. # If telnet shutdown fails tmux shutdown will be used, this risks loss of world save.
  185. else
  186. if [ "${refused}" ]; then
  187. fn_print_error "Graceful: telnet: "
  188. fn_print_fail_eol_nl
  189. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  190. else
  191. fn_print_error_nl "Graceful: telnet: Unknown error"
  192. fn_script_log_error "Graceful: telnet: Unknown error"
  193. fi
  194. echo -en "\n" | tee -a "${lgsmlog}"
  195. echo -en "Telnet output:" | tee -a "${lgsmlog}"
  196. echo -en "\n ${sdtdtelnetshutdown}" | tee -a "${lgsmlog}"
  197. echo -en "\n\n" | tee -a "${lgsmlog}"
  198. fi
  199. else
  200. fn_print_warn "Graceful: telnet: expect not installed: "
  201. fn_print_fail_eol_nl
  202. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  203. fi
  204. }
  205. # Attempts graceful shutdown of Soulmask using telnet.
  206. fn_stop_graceful_sm() {
  207. fn_print_dots "Graceful: telnet"
  208. fn_script_log_info "Graceful: telnet"
  209. if [ "${telnetenabled}" == "false" ]; then
  210. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  211. elif [ "$(command -v expect 2> /dev/null)" ]; then
  212. # Tries to shutdown with both localhost and server IP.
  213. for telnetip in 127.0.0.1 ${ip}; do
  214. fn_print_dots "Graceful: telnet: ${telnetip}:${telnetport}"
  215. fn_script_log_info "Graceful: telnet: ${telnetip}:${telnetport}"
  216. fn_stop_graceful_sm_telnet
  217. completed=$(echo -en "\n ${smtelnetshutdown}" | grep "Completed.")
  218. refused=$(echo -en "\n ${smtelnetshutdown}" | grep "Timeout or EOF")
  219. if [ "${refused}" ]; then
  220. fn_print_error "Graceful: telnet: ${telnetip}:${telnetport} : "
  221. fn_print_fail_eol_nl
  222. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  223. elif [ "${completed}" ]; then
  224. break
  225. fi
  226. done
  227. # If telnet shutdown was successful will use telnet again to check
  228. # the connection has closed, confirming that the tmux session can now be killed.
  229. if [ "${completed}" ]; then
  230. for seconds in {1..30}; do
  231. fn_stop_graceful_sm_telnet
  232. refused=$(echo -en "\n ${smtelnetshutdown}" | grep "Timeout or EOF")
  233. if [ "${refused}" ]; then
  234. fn_print_ok "Graceful: telnet: ${telnetip}:${telnetport} : "
  235. fn_print_ok_eol_nl
  236. fn_script_log_pass "Graceful: telnet: ${telnetip}:${telnetport} : ${seconds} seconds"
  237. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  238. alert="stopped"
  239. alert.sh
  240. fi
  241. break
  242. fi
  243. fn_sleep_time_1
  244. fn_print_dots "Graceful: telnet: ${seconds}"
  245. done
  246. # If telnet shutdown fails, show it and stop
  247. else
  248. if [ "${refused}" ]; then
  249. fn_print_error "Graceful: telnet: "
  250. fn_print_fail_eol_nl
  251. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  252. else
  253. fn_print_error_nl "Graceful: telnet: Unknown error"
  254. fn_script_log_error "Graceful: telnet: Unknown error"
  255. fi
  256. echo -en "\n" | tee -a "${lgsmlog}"
  257. echo -en "Telnet output:" | tee -a "${lgsmlog}"
  258. echo -en "\n ${smtelnetshutdown}" | tee -a "${lgsmlog}"
  259. echo -en "\n\n" | tee -a "${lgsmlog}"
  260. fi
  261. else
  262. fn_print_warn "Graceful: telnet: expect not installed: "
  263. fn_print_fail_eol_nl
  264. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  265. fi
  266. }
  267. # telnet command for soulmask graceful shutdown.
  268. fn_stop_graceful_sm_telnet() {
  269. smtelnetshutdown=$(expect -c '
  270. proc abort {} {
  271. puts "Timeout or EOF\n"
  272. exit 1
  273. }
  274. spawn telnet '"${telnetip}"' '"${telnetport}"'
  275. expect {
  276. "Hello:" { send "saveworld 1\r" }
  277. default abort
  278. }
  279. expect {
  280. "the world is saved." { send "quit 1\r" }
  281. default abort
  282. }
  283. expect {
  284. "World is closing..." {}
  285. default abort
  286. }
  287. expect { eof }
  288. puts "Completed.\n"
  289. ')
  290. }
  291. # Attempts graceful shutdown by sending /save /stop.
  292. fn_stop_graceful_avorion() {
  293. fn_print_dots "Graceful: /save /stop"
  294. fn_script_log_info "Graceful: /save /stop"
  295. # Sends /save.
  296. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1
  297. fn_sleep_time_5
  298. # Sends /quit.
  299. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1
  300. # Waits up to 30 seconds giving the server time to shutdown gracefully.
  301. for seconds in {1..30}; do
  302. check_status.sh
  303. if [ "${status}" == "0" ]; then
  304. fn_print_ok "Graceful: /save /stop: ${seconds}"
  305. fn_print_ok_eol_nl
  306. fn_script_log_pass "Graceful: /save /stop: OK: ${seconds} seconds"
  307. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  308. alert="stopped"
  309. alert.sh
  310. fi
  311. break
  312. fi
  313. fn_sleep_time_1
  314. fn_print_dots "Graceful: /save /stop: ${seconds}"
  315. done
  316. check_status.sh
  317. if [ "${status}" != "0" ]; then
  318. fn_print_error "Graceful: /save /stop: "
  319. fn_print_fail_eol_nl
  320. fn_script_log_error "Graceful: /save /stop: FAIL"
  321. fi
  322. }
  323. fn_stop_graceful_rwr() {
  324. fn_print_dots "Graceful: save_profiles stop_server"
  325. fn_script_log_info "Graceful: save_profiles stop_server"
  326. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" save_profiles ENTER > /dev/null 2>&1
  327. fn_sleep_time_5
  328. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" stop_server ENTER > /dev/null 2>&1
  329. fn_sleep_time_5
  330. fn_stop_graceful_ctrlc
  331. }
  332. fn_stop_graceful_select() {
  333. if [ "${stopmode}" == "1" ]; then
  334. fn_stop_tmux
  335. elif [ "${stopmode}" == "2" ]; then
  336. fn_stop_graceful_ctrlc
  337. elif [ "${stopmode}" == "3" ]; then
  338. fn_stop_graceful_cmd "quit" 30
  339. elif [ "${stopmode}" == "4" ]; then
  340. fn_stop_graceful_cmd "quit" 120
  341. elif [ "${stopmode}" == "5" ]; then
  342. fn_stop_graceful_cmd "stop" 30
  343. elif [ "${stopmode}" == "6" ]; then
  344. fn_stop_graceful_cmd "q" 30
  345. elif [ "${stopmode}" == "7" ]; then
  346. fn_stop_graceful_cmd "exit" 30
  347. elif [ "${stopmode}" == "8" ]; then
  348. fn_stop_graceful_sdtd
  349. elif [ "${stopmode}" == "9" ]; then
  350. fn_stop_graceful_goldsrc
  351. elif [ "${stopmode}" == "10" ]; then
  352. fn_stop_graceful_avorion
  353. elif [ "${stopmode}" == "11" ]; then
  354. fn_stop_graceful_cmd "end" 30
  355. elif [ "${stopmode}" == "12" ]; then
  356. fn_stop_graceful_cmd "shutdown" 30
  357. elif [ "${stopmode}" == "13" ]; then
  358. fn_stop_graceful_sm
  359. elif [ "${stopmode}" == "14" ]; then
  360. fn_stop_graceful_rwr
  361. fi
  362. }
  363. fn_stop_tmux() {
  364. fn_print_dots "${servername}"
  365. fn_script_log_info "tmux kill-session: ${sessionname}: ${servername}"
  366. # Kill tmux session.
  367. TERM=screen tmux -L "${socketname}" kill-session -t "${sessionname}" > /dev/null 2>&1
  368. fn_sleep_time_1
  369. check_status.sh
  370. if [ "${status}" == "0" ]; then
  371. fn_print_ok_nl "${servername}"
  372. fn_script_log_pass "Stopped ${servername}"
  373. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  374. alert="stopped"
  375. alert.sh
  376. fi
  377. else
  378. fn_print_fail_nl "Unable to stop ${servername}"
  379. fn_script_log_fail "Unable to stop ${servername}"
  380. fi
  381. }
  382. # Checks if the server is already stopped.
  383. fn_stop_pre_check() {
  384. if [ "${status}" == "0" ]; then
  385. fn_print_skip_nl "${servername} is already stopped"
  386. fn_script_log_info "${servername} is already stopped"
  387. else
  388. fn_stop_players_online
  389. # Select graceful shutdown.
  390. fn_stop_graceful_select
  391. # Check status again, a kill tmux session if graceful shutdown failed.
  392. check_status.sh
  393. if [ "${status}" != "0" ]; then
  394. fn_stop_tmux
  395. fi
  396. fi
  397. }
  398. fn_print_dots ""
  399. check.sh
  400. # Create a stopping lockfile that only exists while the stop command is running.
  401. date '+%s' > "${lockdir:?}/${selfname}-stopping.lock"
  402. fn_print_dots "${servername}"
  403. info_game.sh
  404. fn_stop_pre_check
  405. # Remove started lockfile.
  406. rm -f "${lockdir:?}/${selfname}-started.lock"
  407. # If user ran the stop command monitor will become disabled.
  408. if [ "${firstcommandname}" == "STOP" ]; then
  409. rm -f "${lockdir:?}/${selfname}-monitoring.lock"
  410. fi
  411. # Remove stopping lockfile.
  412. rm -f "${lockdir:?}/${selfname}-stopping.lock"
  413. if [ -z "${exitbypass}" ]; then
  414. core_exit.sh
  415. fi