command_stop.sh 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #!/bin/bash
  2. # LinuxGSM command_stop.sh function
  3. # Author: Daniel Gibbs
  4. # Contributors: UltimateByte
  5. # Website: https://linuxgsm.com
  6. # Description: Stops the server.
  7. local commandname="STOP"
  8. local commandaction="Stopping"
  9. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  10. # Attempts graceful shutdown by sending 'CTRL+c'.
  11. fn_stop_graceful_ctrlc(){
  12. fn_print_dots "Graceful: CTRL+c"
  13. fn_script_log_info "Graceful: CTRL+c"
  14. # Sends quit.
  15. tmux send-keys -t "${servicename}" C-c > /dev/null 2>&1
  16. # Waits up to 30 seconds giving the server time to shutdown gracefuly.
  17. for seconds in {1..30}; do
  18. check_status.sh
  19. if [ "${status}" == "0" ]; then
  20. fn_print_ok "Graceful: CTRL+c: ${seconds}: "
  21. fn_print_ok_eol_nl
  22. fn_script_log_pass "Graceful: CTRL+c: OK: ${seconds} seconds"
  23. break
  24. fi
  25. sleep 1
  26. fn_print_dots "Graceful: CTRL+c: ${seconds}"
  27. done
  28. check_status.sh
  29. if [ "${status}" != "0" ]; then
  30. fn_print_error "Graceful: CTRL+c: "
  31. fn_print_fail_eol_nl
  32. fn_script_log_error "Graceful: CTRL+c: FAIL"
  33. fi
  34. fn_sleep_time
  35. }
  36. # Attempts graceful shutdown by sending a specified command.
  37. # Usage: fn_stop_graceful_cmd "console_command" "timeout_in_seconds"
  38. # e.g.: fn_stop_graceful_cmd "quit" "30"
  39. fn_stop_graceful_cmd(){
  40. fn_print_dots "Graceful: sending \"${1}\""
  41. fn_script_log_info "Graceful: sending \"${1}\""
  42. # Sends specific stop command.
  43. tmux send -t "${servicename}" "${1}" ENTER > /dev/null 2>&1
  44. # Waits up to ${seconds} seconds giving the server time to shutdown gracefully.
  45. for ((seconds=1; seconds<=${2}; seconds++)); do
  46. check_status.sh
  47. if [ "${status}" == "0" ]; then
  48. fn_print_ok "Graceful: sending \"${1}\": ${seconds}: "
  49. fn_print_ok_eol_nl
  50. fn_script_log_pass "Graceful: sending \"${1}\": OK: ${seconds} seconds"
  51. break
  52. fi
  53. sleep 1
  54. fn_print_dots "Graceful: sending \"${1}\": ${seconds}"
  55. done
  56. check_status.sh
  57. if [ "${status}" != "0" ]; then
  58. fn_print_error "Graceful: sending \"${1}\": "
  59. fn_print_fail_eol_nl
  60. fn_script_log_error "Graceful: sending \"${1}\": FAIL"
  61. fi
  62. fn_sleep_time
  63. }
  64. # Attempts graceful shutdown of goldsource using rcon 'quit' command.
  65. # There is only a 3 second delay before a forced a tmux shutdown
  66. # as Goldsource servers 'quit' command does a restart rather than shutdown.
  67. fn_stop_graceful_goldsource(){
  68. fn_print_dots "Graceful: sending \"quit\""
  69. fn_script_log_info "Graceful: sending \"quit\""
  70. # sends quit
  71. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  72. # Waits 3 seconds as goldsource servers restart with the quit command.
  73. for seconds in {1..3}; do
  74. sleep 1
  75. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  76. done
  77. fn_print_ok "Graceful: sending \"quit\": ${seconds}: "
  78. fn_print_ok_eol_nl
  79. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  80. }
  81. fn_stop_telnet_sdtd(){
  82. if [ -z "${telnetpass}" ]||[ "${telnetpass}" == "NOT SET" ]; then
  83. sdtd_telnet_shutdown=$( expect -c '
  84. proc abort {} {
  85. puts "Timeout or EOF\n"
  86. exit 1
  87. }
  88. spawn telnet '"${telnetip}"' '"${telnetport}"'
  89. expect {
  90. "session." { send "shutdown\r" }
  91. default abort
  92. }
  93. expect { eof }
  94. puts "Completed.\n"
  95. ')
  96. else
  97. sdtd_telnet_shutdown=$( expect -c '
  98. proc abort {} {
  99. puts "Timeout or EOF\n"
  100. exit 1
  101. }
  102. spawn telnet '"${telnetip}"' '"${telnetport}"'
  103. expect {
  104. "password:" { send "'"${telnetpass}"'\r" }
  105. default abort
  106. }
  107. expect {
  108. "session." { send "shutdown\r" }
  109. default abort
  110. }
  111. expect { eof }
  112. puts "Completed.\n"
  113. ')
  114. fi
  115. }
  116. # Attempts graceful shutdown of 7 Days To Die using telnet.
  117. fn_stop_graceful_sdtd(){
  118. fn_print_dots "Graceful: telnet"
  119. fn_script_log_info "Graceful: telnet"
  120. if [ "${telnetenabled}" == "false" ]; then
  121. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  122. elif [ "$(command -v expect 2>/dev/null)" ]; then
  123. # Tries to shutdown with both localhost and server IP.
  124. for telnetip in 127.0.0.1 ${ip}; do
  125. fn_print_dots "Graceful: telnet: ${telnetip}:${telnetport}"
  126. fn_script_log_info "Graceful: telnet: ${telnetip}:${telnetport}"
  127. fn_stop_telnet_sdtd
  128. completed=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Completed.")
  129. refused=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Timeout or EOF")
  130. if [ -n "${refused}" ]; then
  131. fn_print_error "Graceful: telnet: ${telnetip}:${telnetport} : "
  132. fn_print_fail_eol_nl
  133. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  134. elif [ -n "${completed}" ]; then
  135. break
  136. fi
  137. done
  138. # If telnet shutdown was successful will use telnet again to check
  139. # the connection has closed, confirming that the tmux session can now be killed.
  140. if [ -n "${completed}" ]; then
  141. for seconds in {1..30}; do
  142. fn_stop_telnet_sdtd
  143. refused=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Timeout or EOF")
  144. if [ -n "${refused}" ]; then
  145. fn_print_ok "Graceful: telnet: ${telnetip}:${telnetport} : "
  146. fn_print_ok_eol_nl
  147. fn_script_log_pass "Graceful: telnet: ${telnetip}:${telnetport} : ${seconds} seconds"
  148. break
  149. fi
  150. sleep 1
  151. fn_print_dots "Graceful: telnet: ${seconds}"
  152. done
  153. # If telnet shutdown fails tmux shutdown will be used, this risks loss of world save.
  154. else
  155. if [ -n "${refused}" ]; then
  156. fn_print_error "Graceful: telnet: "
  157. fn_print_fail_eol_nl
  158. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  159. else
  160. fn_print_error_nl "Graceful: telnet: Unknown error"
  161. fn_script_log_error "Graceful: telnet: Unknown error"
  162. fi
  163. echo -en "\n" | tee -a "${lgsmlog}"
  164. echo -en "Telnet output:" | tee -a "${lgsmlog}"
  165. echo -en "\n ${sdtd_telnet_shutdown}" | tee -a "${lgsmlog}"
  166. echo -en "\n\n" | tee -a "${lgsmlog}"
  167. fi
  168. else
  169. fn_print_warn "Graceful: telnet: expect not installed: "
  170. fn_print_fail_eol_nl
  171. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  172. fi
  173. fn_sleep_time
  174. }
  175. fn_stop_graceful_select(){
  176. if [ "${shortname}" == "sdtd" ]; then
  177. fn_stop_graceful_sdtd
  178. elif [ "${engine}" == "spark" ]; then
  179. fn_stop_graceful_cmd "q" 30
  180. elif [ "${shortname}" == "terraria" ]; then
  181. fn_stop_graceful_cmd "exit" 30
  182. elif [ "${shortname}" == "mc" ]; then
  183. fn_stop_graceful_cmd "stop" 30
  184. elif [ "${shortname}" == "mta" ]; then
  185. # Long wait time required for mta
  186. # as resources shutdown individually.
  187. fn_stop_graceful_cmd "quit" 120
  188. elif [ "${engine}" == "goldsource" ]; then
  189. fn_stop_graceful_goldsource
  190. elif [ "${engine}" == "unity3d" ]||[ "${engine}" == "unreal4" ]||[ "${engine}" == "unreal3" ]||[ "${engine}" == "unreal2" ]||[ "${engine}" == "unreal" ]||[ "${shortname}" == "fctr" ]||[ "${shortname}" == "mumble" ]||[ "${shortname}" == "wurm" ]||[ "${shortname}" == "jc2" ]||[ "${shortname}" == "jc3" ]||[ "${shortname}" == "sol" ]; then
  191. fn_stop_graceful_ctrlc
  192. elif [ "${engine}" == "source" ]||[ "${engine}" == "quake" ]||[ "${engine}" == "idtech2" ]||[ "${engine}" == "idtech3" ]||[ "${engine}" == "idtech3_ql" ]||[ "${shortname}" == "pz" ]||[ "${shortname}" == "rw" ]; then
  193. fn_stop_graceful_cmd "quit" 30
  194. fi
  195. }
  196. fn_stop_ark(){
  197. # The maximum number of times to check if the ark pid has closed gracefully.
  198. maxpiditer=15
  199. info_config.sh
  200. if [ -z "${queryport}" ]; then
  201. fn_print_warn "No queryport found using info_config.sh"
  202. fn_script_log_warn "No queryport found using info_config.sh"
  203. userconfigfile="${serverfiles}"
  204. userconfigfile+="/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini"
  205. queryport=$(grep ^QueryPort= ${userconfigfile} | cut -d= -f2 | sed "s/[^[:digit:].*].*//g")
  206. fi
  207. if [ -z "${queryport}" ]; then
  208. fn_print_warn "No queryport found in the GameUsersettings.ini file"
  209. fn_script_log_warn "No queryport found in the GameUsersettings.ini file"
  210. return
  211. fi
  212. if [ "${#queryport}" -gt 0 ] ; then
  213. for (( pidcheck=0 ; pidcheck < ${maxpiditer} ; pidcheck++ )) ; do
  214. pid=$(netstat -nap 2>/dev/null | grep "^udp[[:space:]]" | grep ":${queryport}[[:space:]]" | rev | awk '{print $1}' | rev | cut -d\/ -f1)
  215. # Check for a valid pid.
  216. pid=${pid//[!0-9]/}
  217. let pid+=0 # turns an empty string into a valid number, '0',
  218. # and a valid numeric pid remains unchanged.
  219. if [ "${pid}" -gt 1 ]&&[ "${pid}" -le "$(cat "/proc/sys/kernel/pid_max")" ]; then
  220. fn_print_dots "Process still bound. Awaiting graceful exit: ${pidcheck}"
  221. else
  222. break
  223. fi
  224. done
  225. if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then
  226. # The process doesn't want to close after 20 seconds.
  227. # kill it hard.
  228. fn_print_error "Terminating reluctant Ark process: ${pid}"
  229. kill -9 ${pid}
  230. fi
  231. fi
  232. }
  233. fn_stop_teamspeak3(){
  234. fn_print_dots "${servername}"
  235. "${serverfiles}"/ts3server_startscript.sh stop > /dev/null 2>&1
  236. check_status.sh
  237. if [ "${status}" == "0" ]; then
  238. rm -f "${rootdir}/${lockselfname}"
  239. fn_print_ok_nl "${servername}"
  240. fn_script_log_pass "Stopped ${servername}"
  241. else
  242. fn_print_fail_nl "Unable to stop ${servername}"
  243. fn_script_log_error "Unable to stop ${servername}"
  244. fi
  245. }
  246. fn_stop_tmux(){
  247. fn_print_dots "${servername}"
  248. fn_script_log_info "tmux kill-session: ${servername}"
  249. # Kill tmux session
  250. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  251. fn_sleep_time
  252. check_status.sh
  253. if [ "${status}" == "0" ]; then
  254. # ARK does not clean up immediately after tmux is killed.
  255. # Make certain the ports are cleared before continuing.
  256. if [ "${shortname}" == "ark" ]; then
  257. fn_stop_ark
  258. fi
  259. fn_print_ok_nl "${servername}"
  260. fn_script_log_pass "Stopped ${servername}"
  261. else
  262. fn_print_fail_nl "Unable to stop ${servername}"
  263. fn_script_log_fatal "Unable to stop ${servername}"
  264. fi
  265. }
  266. # Checks if the server is already stopped.
  267. fn_stop_pre_check(){
  268. if [ "${status}" == "0" ]; then
  269. fn_print_info_nl "${servername} is already stopped"
  270. fn_script_log_error "${servername} is already stopped"
  271. elif [ "${shortname}" == "ts3" ]; then
  272. fn_stop_teamspeak3
  273. else
  274. fn_stop_graceful_select
  275. fi
  276. # Check status again, a stop tmux session if needed.
  277. check_status.sh
  278. if [ "${status}" != "0" ]; then
  279. fn_stop_tmux
  280. fi
  281. }
  282. fn_print_dots "${servername}"
  283. check.sh
  284. info_config.sh
  285. fn_stop_pre_check
  286. # Remove lockfile.
  287. if [ -f "${rootdir}/${lockselfname}" ]; then
  288. rm -f "${rootdir}/${lockselfname}"
  289. fi
  290. if [ -z "${exitbypass}" ]; then
  291. core_exit.sh
  292. fi