command_stop.sh 10 KB

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