command_stop.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 the '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 given 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 of goldsource using rcon 'quit' command.
  65. # Goldsource 'quit' command restarts rather than shutdown
  66. # this function will only wait 3 seconds then force a tmux shutdown.
  67. # preventing the server from coming back online.
  68. fn_stop_graceful_goldsource(){
  69. fn_print_dots "Graceful: sending \"quit\""
  70. fn_script_log_info "Graceful: sending \"quit\""
  71. # sends quit
  72. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  73. # waits 3 seconds as goldsource servers restart with the quit command
  74. for seconds in {1..3}; do
  75. sleep 1
  76. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  77. done
  78. fn_print_ok "Graceful: sending \"quit\": ${seconds}: "
  79. fn_print_ok_eol_nl
  80. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  81. }
  82. # Attempts graceful of 7 Days To Die using telnet.
  83. fn_stop_telnet_sdtd(){
  84. if [ -z "${telnetpass}" ]||[ "${telnetpass}" == "NOT SET" ]; then
  85. sdtd_telnet_shutdown=$( expect -c '
  86. proc abort {} {
  87. puts "Timeout or EOF\n"
  88. exit 1
  89. }
  90. spawn telnet '"${telnetip}"' '"${telnetport}"'
  91. expect {
  92. "session." { send "shutdown\r" }
  93. default abort
  94. }
  95. expect { eof }
  96. puts "Completed.\n"
  97. ')
  98. else
  99. sdtd_telnet_shutdown=$( expect -c '
  100. proc abort {} {
  101. puts "Timeout or EOF\n"
  102. exit 1
  103. }
  104. spawn telnet '"${telnetip}"' '"${telnetport}"'
  105. expect {
  106. "password:" { send "'"${telnetpass}"'\r" }
  107. default abort
  108. }
  109. expect {
  110. "session." { send "shutdown\r" }
  111. default abort
  112. }
  113. expect { eof }
  114. puts "Completed.\n"
  115. ')
  116. fi
  117. }
  118. fn_stop_graceful_sdtd(){
  119. fn_print_dots "Graceful: telnet"
  120. fn_script_log_info "Graceful: telnet"
  121. if [ "${telnetenabled}" == "false" ]; then
  122. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  123. elif [ "$(command -v expect 2>/dev/null)" ]; then
  124. # Tries to shutdown with both localhost and server IP.
  125. for telnetip in 127.0.0.1 ${ip}; do
  126. fn_print_dots "Graceful: telnet: ${telnetip}:${telnetport}"
  127. fn_script_log_info "Graceful: telnet: ${telnetip}:${telnetport}"
  128. fn_stop_telnet_sdtd
  129. completed=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Completed.")
  130. refused=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Timeout or EOF")
  131. if [ -n "${refused}" ]; then
  132. fn_print_error "Graceful: telnet: ${telnetip}:${telnetport} : "
  133. fn_print_fail_eol_nl
  134. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  135. elif [ -n "${completed}" ]; then
  136. break
  137. fi
  138. done
  139. # If telnet was successful will use telnet again to check the connection has closed
  140. # This confirms that the tmux session can now be killed.
  141. if [ -n "${completed}" ]; then
  142. for seconds in {1..30}; do
  143. fn_stop_telnet_sdtd
  144. refused=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Timeout or EOF")
  145. if [ -n "${refused}" ]; then
  146. fn_print_ok "Graceful: telnet: ${telnetip}:${telnetport} : "
  147. fn_print_ok_eol_nl
  148. fn_script_log_pass "Graceful: telnet: ${telnetip}:${telnetport} : ${seconds} seconds"
  149. break
  150. fi
  151. sleep 1
  152. fn_print_dots "Graceful: telnet: ${seconds}"
  153. done
  154. # If telnet failed will go straight to tmux shutdown.
  155. # If cannot shutdown correctly world save may be lost
  156. else
  157. if [ -n "${refused}" ]; then
  158. fn_print_error "Graceful: telnet: "
  159. fn_print_fail_eol_nl
  160. fn_script_log_error "Graceful: telnet: ${telnetip}:${telnetport} : FAIL"
  161. else
  162. fn_print_error_nl "Graceful: telnet: Unknown error"
  163. fn_script_log_error "Graceful: telnet: Unknown error"
  164. fi
  165. echo -en "\n" | tee -a "${lgsmlog}"
  166. echo -en "Telnet output:" | tee -a "${lgsmlog}"
  167. echo -en "\n ${sdtd_telnet_shutdown}" | tee -a "${lgsmlog}"
  168. echo -en "\n\n" | tee -a "${lgsmlog}"
  169. fi
  170. else
  171. fn_print_warn "Graceful: telnet: expect not installed: "
  172. fn_print_fail_eol_nl
  173. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  174. fi
  175. fn_sleep_time
  176. }
  177. fn_stop_graceful_select(){
  178. if [ "${shortname}" == "sdtd" ]; then
  179. fn_stop_graceful_sdtd
  180. elif [ "${engine}" == "Spark" ]; then
  181. fn_stop_graceful_cmd "q" 30
  182. elif [ "${shortname}" == "terraria" ]; then
  183. fn_stop_graceful_cmd "exit" 30
  184. elif [ "${shortname}" == "mc" ]; then
  185. fn_stop_graceful_cmd "stop" 30
  186. elif [ "${shortname}" == "mta" ]; then
  187. # we need a long wait time here as resources are stopped individually and process their own shutdowns
  188. fn_stop_graceful_cmd "quit" 120
  189. elif [ "${engine}" == "goldsource" ]; then
  190. fn_stop_graceful_goldsource
  191. elif [ "${engine}" == "unity3d" ]||[ "${engine}" == "unreal4" ]||[ "${engine}" == "unreal3" ]||[ "${engine}" == "unreal2" ]||[ "${engine}" == "unreal" ]||[ "${shortname}" == "fctr" ]||[ "${shortname}" == "mumble" ]||[ "${shortname}" == "wurm" ]||[ "${shortname}" == "jc2" ]||[ "${shortname}" == "jc3" ]; then
  192. fn_stop_graceful_ctrlc
  193. elif [ "${engine}" == "source" ]||[ "${engine}" == "quake" ]||[ "${engine}" == "idtech2" ]||[ "${engine}" == "idtech3" ]||[ "${engine}" == "idtech3_ql" ]||[ "${shortname}" == "jc2" ]||[ "${shortname}" == "pz" ]||[ "${shortname}" == "rw" ]; then
  194. fn_stop_graceful_cmd "quit" 30
  195. fi
  196. }
  197. fn_stop_ark(){
  198. maxpiditer=15 # The maximum number of times to check if the ark pid has closed gracefully.
  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. #
  216. # check for a valid pid
  217. pid=${pid//[!0-9]/}
  218. let pid+=0 # turns an empty string into a valid number, '0',
  219. # and a valid numeric pid remains unchanged.
  220. if [ "${pid}" -gt 1 ]&&[ "${pid}" -le "$(cat "/proc/sys/kernel/pid_max")" ]; then
  221. fn_print_dots "Process still bound. Awaiting graceful exit: ${pidcheck}"
  222. else
  223. break # Our job is done here
  224. fi # end if for pid range check
  225. done
  226. if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then
  227. # The process doesn't want to close after 20 seconds.
  228. # kill it hard.
  229. fn_print_error "Terminating reluctant Ark process: ${pid}"
  230. kill -9 ${pid}
  231. fi
  232. fi # end if for port check
  233. } # end of fn_stop_ark
  234. fn_stop_teamspeak3(){
  235. fn_print_dots "${servername}"
  236. "${serverfiles}"/ts3server_startscript.sh stop > /dev/null 2>&1
  237. check_status.sh
  238. if [ "${status}" == "0" ]; then
  239. # Remove lockfile
  240. rm -f "${rootdir}/${lockselfname}"
  241. fn_print_ok_nl "${servername}"
  242. fn_script_log_pass "Stopped ${servername}"
  243. else
  244. fn_print_fail_nl "Unable to stop ${servername}"
  245. fn_script_log_error "Unable to stop ${servername}"
  246. fi
  247. }
  248. fn_stop_tmux(){
  249. fn_print_dots "${servername}"
  250. fn_script_log_info "tmux kill-session: ${servername}"
  251. # Kill tmux session
  252. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  253. fn_sleep_time
  254. check_status.sh
  255. if [ "${status}" == "0" ]; then
  256. # ARK doesn't clean up immediately after tmux is killed.
  257. # Make certain the ports are cleared before continuing.
  258. if [ "${shortname}" == "ark" ]; then
  259. fn_stop_ark
  260. fi
  261. fn_print_ok_nl "${servername}"
  262. fn_script_log_pass "Stopped ${servername}"
  263. else
  264. fn_print_fail_nl "Unable to stop ${servername}"
  265. fn_script_log_fatal "Unable to stop ${servername}"
  266. fi
  267. }
  268. # checks if the server is already stopped before trying to stop.
  269. fn_stop_pre_check(){
  270. # Is the server already stopped
  271. if [ "${status}" == "0" ]; then # $status comes from check_status.sh, which is run by check.sh for this command
  272. fn_print_info_nl "${servername} is already stopped"
  273. fn_script_log_error "${servername} is already stopped"
  274. elif [ "${shortname}" == "ts3" ]; then
  275. fn_stop_teamspeak3
  276. else
  277. fn_stop_graceful_select
  278. fi
  279. # Check status again, a stop tmux session if needed
  280. check_status.sh
  281. if [ "${status}" != "0" ]; then
  282. fn_stop_tmux
  283. fi
  284. }
  285. fn_print_dots "${servername}"
  286. check.sh
  287. info_config.sh
  288. fn_stop_pre_check
  289. # Remove lockfile
  290. if [ -f "${rootdir}/${lockselfname}" ]; then
  291. rm -f "${rootdir}/${lockselfname}"
  292. fi
  293. if [ -z "${exitbypass}" ]; then
  294. core_exit.sh
  295. fi