command_stop.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. # Monitor runs the update once the server is empty.
  23. if [ "${firstcommandname}" == "UPDATE" ]; then
  24. date '+%s' > "${lockdir:?}/${selfname}-update-request.lock"
  25. if [ "${forceupdate}" == "1" ]; then
  26. echo "force" >> "${lockdir:?}/${selfname}-update-request.lock"
  27. fi
  28. fn_print_info_nl "Update postponed until the server is empty"
  29. fn_script_log_info "Update postponed until the server is empty"
  30. fi
  31. fn_stop_cancel_locks
  32. # Exit the whole command, including when called by another command (e.g. update) with exitbypass.
  33. unset exitbypass
  34. core_exit.sh
  35. fi
  36. fi
  37. }
  38. # Removes the lockfiles of the command that called stop when it ends without stopping the server.
  39. fn_stop_cancel_locks() {
  40. if [ "${firstcommandname}" == "UPDATE" ] || [ "${firstcommandname}" == "WORKSHOP-UPDATE" ]; then
  41. rm -f "${lockdir:?}/update.lock" "${lockdir:?}/last-updated.lock"
  42. elif [ "${firstcommandname}" == "VALIDATE" ]; then
  43. rm -f "${lockdir:?}/update.lock"
  44. elif [ "${firstcommandname}" == "BACKUP" ]; then
  45. rm -f "${lockdir:?}/backup.lock"
  46. fi
  47. }
  48. # Tells players a warning countdown was cancelled, used when CTRL-C is pressed.
  49. fn_stop_warn_players_cancel() {
  50. if [ "$(cat "${lockdir}/${selfname}-warning.lock" 2> /dev/null)" == "$$" ]; then
  51. fn_stop_warn_players_send "${warnplayersaction}" "0"
  52. rm -f "${lockdir:?}/${selfname}-warning.lock"
  53. fn_stop_cancel_locks
  54. fi
  55. }
  56. # Sets the game's chat command used to warn players, %s is replaced by the message.
  57. fn_stop_warn_players_command() {
  58. warncommand=""
  59. if [ "${shortname}" == "pz" ]; then
  60. warncommand='servermsg "%s"'
  61. elif [ "${shortname}" == "dst" ]; then
  62. warncommand='c_announce("%s")'
  63. elif [ "${shortname}" == "scpsl" ] || [ "${shortname}" == "scpslsm" ]; then
  64. warncommand="/bc 10 %s"
  65. elif [ "${shortname}" == "vints" ]; then
  66. warncommand="/announce %s"
  67. elif [ "${shortname}" == "wmc" ]; then
  68. warncommand="alert %s"
  69. elif [ "${shortname}" == "fctr" ]; then
  70. warncommand="%s"
  71. elif [ "${engine}" == "source" ] || [ "${engine}" == "source2" ] || [ "${engine}" == "goldsrc" ]; then
  72. warncommand="say %s"
  73. else
  74. local saygames=(bt cod cod2 cod4 coduo codwaw etl hyt jk2 mc mcb mohaa pmc q2 q3 q4 ql qw rtcw sof2 terraria unt wet wf xnt)
  75. local game
  76. for game in "${saygames[@]}"; do
  77. if [ "${shortname}" == "${game}" ]; then
  78. warncommand="say %s"
  79. fi
  80. done
  81. fi
  82. }
  83. # Sends a warning message to players using the game's own chat command.
  84. # A minutes value of 0 sends a cancelled message.
  85. fn_stop_warn_players_send() {
  86. local message="Server ${1} in ${2} minute"
  87. if [ "${2}" != "1" ]; then
  88. message+="s"
  89. fi
  90. if [ "${2}" == "0" ]; then
  91. message="Server ${1} cancelled"
  92. fi
  93. local warnmessage="${warncommand//%s/${message}}"
  94. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" -l "${warnmessage}" > /dev/null 2>&1
  95. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" ENTER > /dev/null 2>&1
  96. }
  97. # Waits for another LinuxGSM command that is already warning players on this server.
  98. fn_stop_warn_players_wait() {
  99. local warnlock="${lockdir}/${selfname}-warning.lock"
  100. local warnpid
  101. while [ -f "${warnlock}" ]; do
  102. warnpid="$(cat "${warnlock}" 2> /dev/null)"
  103. if [ -z "${warnpid}" ] || ! kill -0 "${warnpid}" 2> /dev/null; then
  104. rm -f "${warnlock:?}"
  105. break
  106. fi
  107. fn_print_info_nl "Warning players: another command is already warning players: waiting"
  108. fn_script_log_info "Warning players: another command is already warning players: waiting"
  109. sleep 30
  110. done
  111. }
  112. # Counts down to the stop with in-game warnings, e.g. warnplayerstimer="15" warns at 15, 10, 5, 3 and 1 minutes.
  113. # Skipped when the server is empty, and ends early if the server empties during the countdown.
  114. fn_stop_warn_players() {
  115. if [ "${firstcommandname}" == "STOP" ]; then
  116. warnplayersaction="stopping"
  117. elif [ "${firstcommandname}" == "RESTART" ] || [ "${firstcommandname}" == "BACKUP" ]; then
  118. warnplayersaction="restarting"
  119. elif [ "${firstcommandname}" == "UPDATE" ] || [ "${firstcommandname}" == "WORKSHOP-UPDATE" ]; then
  120. warnplayersaction="updating"
  121. else
  122. return
  123. fi
  124. if [ -z "${warnplayerstimer}" ] || [ "${forceupdate}" == "1" ]; then
  125. return
  126. fi
  127. if ! [[ "${warnplayerstimer}" =~ ^[0-9]+$ ]]; then
  128. fn_print_warn_nl "Warning players: warnplayerstimer must be a number of minutes: skipped"
  129. fn_script_log_warn "Warning players: warnplayerstimer \"${warnplayerstimer}\" is not a number of minutes: skipped"
  130. return
  131. fi
  132. local timer="$((10#${warnplayerstimer}))"
  133. if [ "${timer}" -eq 0 ]; then
  134. return
  135. fi
  136. fn_stop_warn_players_command
  137. if [ "${consoleinteract}" != "yes" ] || [ -z "${warncommand}" ]; then
  138. fn_print_info_nl "Warning players: not supported by ${gamename}: skipped"
  139. fn_script_log_info "Warning players: not supported by ${gamename}: skipped"
  140. return
  141. fi
  142. fn_stop_warn_players_wait
  143. check_status.sh
  144. if [ "${status}" == "0" ]; then
  145. return
  146. fi
  147. local steps=("${timer}")
  148. local step
  149. for step in 60 30 15 10 5 3 1; do
  150. if [ "${step}" -lt "${timer}" ]; then
  151. steps+=("${step}")
  152. fi
  153. done
  154. echo "$$" > "${lockdir:?}/${selfname}-warning.lock"
  155. local i next
  156. for i in "${!steps[@]}"; do
  157. check_status.sh
  158. if [ "${status}" == "0" ]; then
  159. break
  160. fi
  161. # shellcheck disable=SC2034 # Used by check_players_online.sh.
  162. playercheckforce=1
  163. check_players_online.sh
  164. unset playercheckforce
  165. if [ "${playerscounted}" == "1" ] && [ -z "${playersonline}" ]; then
  166. fn_print_info_nl "Warning players: no players on the server"
  167. fn_script_log_info "Warning players: no players on the server"
  168. break
  169. fi
  170. if [ "${i}" == "0" ] && [ -n "${playercountunavailable}" ]; then
  171. fn_print_info_nl "Warning players: unable to check for players (${playercountunavailable}): warning anyway"
  172. fn_script_log_info "Warning players: unable to check for players (${playercountunavailable}): warning anyway"
  173. fi
  174. fn_stop_warn_players_send "${warnplayersaction}" "${steps[i]}"
  175. fn_print_info_nl "Warning players: ${warnplayersaction} in ${steps[i]} minute(s)"
  176. fn_script_log_info "Warning players: ${warnplayersaction} in ${steps[i]} minute(s)"
  177. next="${steps[i + 1]:-0}"
  178. local waitseconds=$(((steps[i] - next) * 60))
  179. while [ "${waitseconds}" -gt 0 ]; do
  180. sleep 10
  181. waitseconds=$((waitseconds - 10))
  182. check_status.sh
  183. if [ "${status}" == "0" ]; then
  184. break 2
  185. fi
  186. done
  187. done
  188. unset warnplayersaction
  189. rm -f "${lockdir:?}/${selfname}-warning.lock"
  190. }
  191. # Attempts graceful shutdown by sending 'CTRL+c'.
  192. fn_stop_graceful_ctrlc() {
  193. fn_print_dots "Graceful: CTRL+c"
  194. fn_script_log_info "Graceful: CTRL+c"
  195. # Sends CTRL+c.
  196. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" C-c > /dev/null 2>&1
  197. # Waits up to 30 seconds giving the server time to shutdown gracefully.
  198. for seconds in {1..30}; do
  199. check_status.sh
  200. if [ "${status}" == "0" ]; then
  201. fn_print_ok "Graceful: CTRL+c: ${seconds}"
  202. fn_print_ok_eol_nl
  203. fn_script_log_pass "Graceful: CTRL+c: OK: ${seconds} seconds"
  204. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  205. alert="stopped"
  206. alert.sh
  207. fi
  208. break
  209. fi
  210. fn_sleep_time_1
  211. fn_print_dots "Graceful: CTRL+c: ${seconds}"
  212. done
  213. check_status.sh
  214. if [ "${status}" != "0" ]; then
  215. fn_print_error "Graceful: CTRL+c: "
  216. fn_print_fail_eol_nl
  217. fn_script_log_error "Graceful: CTRL+c: FAIL"
  218. fi
  219. }
  220. # Attempts graceful shutdown by sending a specified command.
  221. # Usage: fn_stop_graceful_cmd "console_command" "timeout_in_seconds"
  222. # e.g.: fn_stop_graceful_cmd "quit" "30"
  223. fn_stop_graceful_cmd() {
  224. fn_print_dots "Graceful: sending \"${1}\""
  225. fn_script_log_info "Graceful: sending \"${1}\""
  226. # Sends specific stop command.
  227. TERM=screen tmux -L "${socketname}" send -t "${sessionname}" ENTER "${1}" ENTER > /dev/null 2>&1
  228. # Waits up to ${seconds} seconds giving the server time to shutdown gracefully.
  229. for ((seconds = 1; seconds <= ${2}; seconds++)); do
  230. check_status.sh
  231. if [ "${status}" == "0" ]; then
  232. fn_print_ok "Graceful: sending \"${1}\": ${seconds}"
  233. fn_print_ok_eol_nl
  234. fn_script_log_pass "Graceful: sending \"${1}\": OK: ${seconds} seconds"
  235. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  236. alert="stopped"
  237. alert.sh
  238. fi
  239. break
  240. fi
  241. fn_sleep_time_1
  242. fn_print_dots "Graceful: sending \"${1}\": ${seconds}"
  243. done
  244. check_status.sh
  245. if [ "${status}" != "0" ]; then
  246. fn_print_error "Graceful: sending \"${1}\": "
  247. fn_print_fail_eol_nl
  248. fn_script_log_error "Graceful: sending \"${1}\": FAIL"
  249. fi
  250. }
  251. # Attempts graceful shutdown of goldsrc using rcon 'quit' command.
  252. # There is only a 3 second delay before a forced a tmux shutdown
  253. # as GoldSrc servers 'quit' command does a restart rather than shutdown.
  254. fn_stop_graceful_goldsrc() {
  255. fn_print_dots "Graceful: sending \"quit\""
  256. fn_script_log_info "Graceful: sending \"quit\""
  257. # sends quit
  258. TERM=screen tmux -L "${socketname}" send -t "${sessionname}" quit ENTER > /dev/null 2>&1
  259. # Waits 3 seconds as goldsrc servers restart with the quit command.
  260. for seconds in {1..3}; do
  261. fn_sleep_time_1
  262. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  263. done
  264. fn_print_ok "Graceful: sending \"quit\": ${seconds}"
  265. fn_print_ok_eol_nl
  266. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  267. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  268. alert="stopped"
  269. alert.sh
  270. fi
  271. }
  272. # telnet command for sdtd graceful shutdown.
  273. fn_stop_graceful_sdtd_telnet() {
  274. if [ -z "${telnetpassword}" ] || [ "${telnetpassword}" == "NOT SET" ]; then
  275. sdtdtelnetshutdown=$(expect -c '
  276. proc abort {} {
  277. puts "Timeout or EOF\n"
  278. exit 1
  279. }
  280. spawn telnet '"${telnetip}"' '"${telnetport}"'
  281. expect {
  282. "session." { send "shutdown\r" }
  283. default abort
  284. }
  285. expect { eof }
  286. puts "Completed.\n"
  287. ')
  288. else
  289. sdtdtelnetshutdown=$(expect -c '
  290. proc abort {} {
  291. puts "Timeout or EOF\n"
  292. exit 1
  293. }
  294. spawn telnet '"${telnetip}"' '"${telnetport}"'
  295. expect {
  296. "password:" { send "'"${telnetpassword}"'\r" }
  297. default abort
  298. }
  299. expect {
  300. "session." { send "shutdown\r" }
  301. default abort
  302. }
  303. expect { eof }
  304. puts "Completed.\n"
  305. ')
  306. fi
  307. }
  308. # Attempts graceful shutdown of 7 Days To Die using telnet.
  309. fn_stop_graceful_sdtd() {
  310. fn_print_dots "Graceful: telnet"
  311. fn_script_log_info "Graceful: telnet"
  312. if [ "${telnetenabled}" == "false" ]; then
  313. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  314. elif [ "$(command -v expect 2> /dev/null)" ]; then
  315. # Tries to shutdown with both localhost and server IP.
  316. for telnetip in 127.0.0.1 ${ip}; do
  317. fn_print_dots "Graceful: telnet: ${telnetip}:${telnetport}"
  318. fn_script_log_info "Graceful: telnet: ${telnetip}:${telnetport}"
  319. fn_stop_graceful_sdtd_telnet
  320. completed=$(echo -en "\n ${sdtdtelnetshutdown}" | grep "Completed.")
  321. refused=$(echo -en "\n ${sdtdtelnetshutdown}" | grep "Timeout or EOF")
  322. if [ "${refused}" ]; then
  323. fn_print_error "Graceful: telnet: ${telnetip}:${telnetport} : "
  324. fn_print_fail_eol_nl
  325. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  326. elif [ "${completed}" ]; then
  327. break
  328. fi
  329. done
  330. # If telnet shutdown was successful will use telnet again to check
  331. # the connection has closed, confirming that the tmux session can now be killed.
  332. if [ "${completed}" ]; then
  333. for seconds in {1..30}; do
  334. fn_stop_graceful_sdtd_telnet
  335. refused=$(echo -en "\n ${sdtdtelnetshutdown}" | grep "Timeout or EOF")
  336. if [ "${refused}" ]; then
  337. fn_print_ok "Graceful: telnet: ${telnetip}:${telnetport} : "
  338. fn_print_ok_eol_nl
  339. fn_script_log_pass "Graceful: telnet: ${telnetip}:${telnetport} : ${seconds} seconds"
  340. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  341. alert="stopped"
  342. alert.sh
  343. fi
  344. break
  345. fi
  346. fn_sleep_time_1
  347. fn_print_dots "Graceful: telnet: ${seconds}"
  348. done
  349. # If telnet shutdown fails tmux shutdown will be used, this risks loss of world save.
  350. else
  351. if [ "${refused}" ]; then
  352. fn_print_error "Graceful: telnet: "
  353. fn_print_fail_eol_nl
  354. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  355. else
  356. fn_print_error_nl "Graceful: telnet: Unknown error"
  357. fn_script_log_error "Graceful: telnet: Unknown error"
  358. fi
  359. echo -en "\n" | tee -a "${lgsmlog}"
  360. echo -en "Telnet output:" | tee -a "${lgsmlog}"
  361. echo -en "\n ${sdtdtelnetshutdown}" | tee -a "${lgsmlog}"
  362. echo -en "\n\n" | tee -a "${lgsmlog}"
  363. fi
  364. else
  365. fn_print_warn "Graceful: telnet: expect not installed: "
  366. fn_print_fail_eol_nl
  367. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  368. fi
  369. }
  370. # Attempts graceful shutdown of Soulmask using telnet.
  371. fn_stop_graceful_sm() {
  372. fn_print_dots "Graceful: telnet"
  373. fn_script_log_info "Graceful: telnet"
  374. if [ "${telnetenabled}" == "false" ]; then
  375. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  376. elif [ "$(command -v expect 2> /dev/null)" ]; then
  377. # Tries to shutdown with both localhost and server IP.
  378. for telnetip in 127.0.0.1 ${ip}; do
  379. fn_print_dots "Graceful: telnet: ${telnetip}:${telnetport}"
  380. fn_script_log_info "Graceful: telnet: ${telnetip}:${telnetport}"
  381. fn_stop_graceful_sm_telnet
  382. completed=$(echo -en "\n ${smtelnetshutdown}" | grep "Completed.")
  383. refused=$(echo -en "\n ${smtelnetshutdown}" | grep "Timeout or EOF")
  384. if [ "${refused}" ]; then
  385. fn_print_error "Graceful: telnet: ${telnetip}:${telnetport} : "
  386. fn_print_fail_eol_nl
  387. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  388. elif [ "${completed}" ]; then
  389. break
  390. fi
  391. done
  392. # If telnet shutdown was successful will use telnet again to check
  393. # the connection has closed, confirming that the tmux session can now be killed.
  394. if [ "${completed}" ]; then
  395. for seconds in {1..30}; do
  396. fn_stop_graceful_sm_telnet
  397. refused=$(echo -en "\n ${smtelnetshutdown}" | grep "Timeout or EOF")
  398. if [ "${refused}" ]; then
  399. fn_print_ok "Graceful: telnet: ${telnetip}:${telnetport} : "
  400. fn_print_ok_eol_nl
  401. fn_script_log_pass "Graceful: telnet: ${telnetip}:${telnetport} : ${seconds} seconds"
  402. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  403. alert="stopped"
  404. alert.sh
  405. fi
  406. break
  407. fi
  408. fn_sleep_time_1
  409. fn_print_dots "Graceful: telnet: ${seconds}"
  410. done
  411. # If telnet shutdown fails, show it and stop
  412. else
  413. if [ "${refused}" ]; then
  414. fn_print_error "Graceful: telnet: "
  415. fn_print_fail_eol_nl
  416. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  417. else
  418. fn_print_error_nl "Graceful: telnet: Unknown error"
  419. fn_script_log_error "Graceful: telnet: Unknown error"
  420. fi
  421. echo -en "\n" | tee -a "${lgsmlog}"
  422. echo -en "Telnet output:" | tee -a "${lgsmlog}"
  423. echo -en "\n ${smtelnetshutdown}" | tee -a "${lgsmlog}"
  424. echo -en "\n\n" | tee -a "${lgsmlog}"
  425. fi
  426. else
  427. fn_print_warn "Graceful: telnet: expect not installed: "
  428. fn_print_fail_eol_nl
  429. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  430. fi
  431. }
  432. # telnet command for soulmask graceful shutdown.
  433. fn_stop_graceful_sm_telnet() {
  434. smtelnetshutdown=$(expect -c '
  435. proc abort {} {
  436. puts "Timeout or EOF\n"
  437. exit 1
  438. }
  439. spawn telnet '"${telnetip}"' '"${telnetport}"'
  440. expect {
  441. "Hello:" { send "saveworld 1\r" }
  442. default abort
  443. }
  444. expect {
  445. "the world is saved." { send "quit 1\r" }
  446. default abort
  447. }
  448. expect {
  449. "World is closing..." {}
  450. default abort
  451. }
  452. expect { eof }
  453. puts "Completed.\n"
  454. ')
  455. }
  456. # Attempts graceful shutdown by sending /save /stop.
  457. fn_stop_graceful_avorion() {
  458. fn_print_dots "Graceful: /save /stop"
  459. fn_script_log_info "Graceful: /save /stop"
  460. # Sends /save.
  461. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" /save ENTER > /dev/null 2>&1
  462. fn_sleep_time_5
  463. # Sends /quit.
  464. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" /stop ENTER > /dev/null 2>&1
  465. # Waits up to 30 seconds giving the server time to shutdown gracefully.
  466. for seconds in {1..30}; do
  467. check_status.sh
  468. if [ "${status}" == "0" ]; then
  469. fn_print_ok "Graceful: /save /stop: ${seconds}"
  470. fn_print_ok_eol_nl
  471. fn_script_log_pass "Graceful: /save /stop: OK: ${seconds} seconds"
  472. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  473. alert="stopped"
  474. alert.sh
  475. fi
  476. break
  477. fi
  478. fn_sleep_time_1
  479. fn_print_dots "Graceful: /save /stop: ${seconds}"
  480. done
  481. check_status.sh
  482. if [ "${status}" != "0" ]; then
  483. fn_print_error "Graceful: /save /stop: "
  484. fn_print_fail_eol_nl
  485. fn_script_log_error "Graceful: /save /stop: FAIL"
  486. fi
  487. }
  488. fn_stop_graceful_rwr() {
  489. fn_print_dots "Graceful: save_profiles stop_server"
  490. fn_script_log_info "Graceful: save_profiles stop_server"
  491. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" save_profiles ENTER > /dev/null 2>&1
  492. fn_sleep_time_5
  493. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" stop_server ENTER > /dev/null 2>&1
  494. fn_sleep_time_5
  495. fn_stop_graceful_ctrlc
  496. }
  497. fn_stop_graceful_select() {
  498. if [ "${stopmode}" == "1" ]; then
  499. fn_stop_tmux
  500. elif [ "${stopmode}" == "2" ]; then
  501. fn_stop_graceful_ctrlc
  502. elif [ "${stopmode}" == "3" ]; then
  503. fn_stop_graceful_cmd "quit" 30
  504. elif [ "${stopmode}" == "4" ]; then
  505. fn_stop_graceful_cmd "quit" 120
  506. elif [ "${stopmode}" == "5" ]; then
  507. fn_stop_graceful_cmd "stop" 30
  508. elif [ "${stopmode}" == "6" ]; then
  509. fn_stop_graceful_cmd "q" 30
  510. elif [ "${stopmode}" == "7" ]; then
  511. fn_stop_graceful_cmd "exit" 30
  512. elif [ "${stopmode}" == "8" ]; then
  513. fn_stop_graceful_sdtd
  514. elif [ "${stopmode}" == "9" ]; then
  515. fn_stop_graceful_goldsrc
  516. elif [ "${stopmode}" == "10" ]; then
  517. fn_stop_graceful_avorion
  518. elif [ "${stopmode}" == "11" ]; then
  519. fn_stop_graceful_cmd "end" 30
  520. elif [ "${stopmode}" == "12" ]; then
  521. fn_stop_graceful_cmd "shutdown" 30
  522. elif [ "${stopmode}" == "13" ]; then
  523. fn_stop_graceful_sm
  524. elif [ "${stopmode}" == "14" ]; then
  525. fn_stop_graceful_rwr
  526. fi
  527. }
  528. fn_stop_tmux() {
  529. fn_print_dots "${servername}"
  530. fn_script_log_info "tmux kill-session: ${sessionname}: ${servername}"
  531. # Kill tmux session.
  532. TERM=screen tmux -L "${socketname}" kill-session -t "${sessionname}" > /dev/null 2>&1
  533. fn_sleep_time_1
  534. check_status.sh
  535. if [ "${status}" == "0" ]; then
  536. fn_print_ok_nl "${servername}"
  537. fn_script_log_pass "Stopped ${servername}"
  538. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "STOP" ]; then
  539. alert="stopped"
  540. alert.sh
  541. fi
  542. else
  543. fn_print_fail_nl "Unable to stop ${servername}"
  544. fn_script_log_fail "Unable to stop ${servername}"
  545. fi
  546. }
  547. # Checks if the server is already stopped.
  548. fn_stop_pre_check() {
  549. if [ "${status}" == "0" ]; then
  550. fn_print_skip_nl "${servername} is already stopped"
  551. fn_script_log_info "${servername} is already stopped"
  552. else
  553. # Select graceful shutdown.
  554. fn_stop_graceful_select
  555. # Check status again, a kill tmux session if graceful shutdown failed.
  556. check_status.sh
  557. if [ "${status}" != "0" ]; then
  558. fn_stop_tmux
  559. fi
  560. fi
  561. }
  562. fn_print_dots ""
  563. check.sh
  564. info_game.sh
  565. # Players are checked and warned before the stopping lockfile is created,
  566. # as a warning countdown can outlast monitor's stale lockfile check.
  567. if [ "${status}" != "0" ]; then
  568. fn_stop_players_online
  569. fn_stop_warn_players
  570. check_status.sh
  571. fi
  572. # Create a stopping lockfile that only exists while the stop command is running.
  573. date '+%s' > "${lockdir:?}/${selfname}-stopping.lock"
  574. fn_print_dots "${servername}"
  575. fn_stop_pre_check
  576. # Remove started lockfile.
  577. rm -f "${lockdir:?}/${selfname}-started.lock"
  578. # If user ran the stop command monitor will become disabled.
  579. if [ "${firstcommandname}" == "STOP" ]; then
  580. rm -f "${lockdir:?}/${selfname}-monitoring.lock"
  581. fi
  582. # Remove stopping lockfile.
  583. rm -f "${lockdir:?}/${selfname}-stopping.lock"
  584. if [ -z "${exitbypass}" ]; then
  585. core_exit.sh
  586. fi