command_stop.sh 10 KB

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