command_stop.sh 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 C-c -t "${servicename}" > /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. fn_stop_tmux
  36. }
  37. # Attempts graceful shutdown by sending a specified command.
  38. # Usage: fn_stop_graceful_cmd "console_command" "timeout_in_seconds"
  39. # e.g.: fn_stop_graceful_cmd "quit" "30"
  40. fn_stop_graceful_cmd(){
  41. fn_print_dots "Graceful: sending \"${1}\""
  42. fn_script_log_info "Graceful: sending \"${1}\""
  43. # sends specific stop command
  44. tmux send -t "${servicename}" "${1}" ENTER > /dev/null 2>&1
  45. # waits up to given seconds giving the server time to shutdown gracefully
  46. for ((seconds=1; seconds<=${2}; seconds++)); do
  47. check_status.sh
  48. if [ "${status}" == "0" ]; then
  49. fn_print_ok "Graceful: sending \"${1}\": ${seconds}: "
  50. fn_print_ok_eol_nl
  51. fn_script_log_pass "Graceful: sending \"${1}\": OK: ${seconds} seconds"
  52. break
  53. fi
  54. sleep 1
  55. fn_print_dots "Graceful: sending \"${1}\": ${seconds}"
  56. done
  57. check_status.sh
  58. if [ "${status}" != "0" ]; then
  59. fn_print_error "Graceful: sending \"${1}\": "
  60. fn_print_fail_eol_nl
  61. fn_script_log_error "Graceful: sending \"${1}\": FAIL"
  62. fi
  63. sleep 0.5
  64. fn_stop_tmux
  65. }
  66. # Attempts graceful of goldsource using rcon 'quit' command.
  67. # Goldsource 'quit' command restarts rather than shutdown
  68. # this function will only wait 3 seconds then force a tmux shutdown.
  69. # preventing the server from coming back online.
  70. fn_stop_graceful_goldsource(){
  71. fn_print_dots "Graceful: sending \"quit\""
  72. fn_script_log_info "Graceful: sending \"quit\""
  73. # sends quit
  74. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  75. # waits 3 seconds as goldsource servers restart with the quit command
  76. for seconds in {1..3}; do
  77. sleep 1
  78. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  79. done
  80. fn_print_ok "Graceful: sending \"quit\": ${seconds}: "
  81. fn_print_ok_eol_nl
  82. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  83. sleep 0.5
  84. fn_stop_tmux
  85. }
  86. # Attempts graceful of 7 Days To Die using telnet.
  87. fn_stop_telnet_sdtd(){
  88. if [ -z "${telnetpass}" ]; then
  89. telnetpass="NOTSET"
  90. fi
  91. sdtd_telnet_shutdown=$( expect -c '
  92. proc abort {} {
  93. puts "Timeout or EOF\n"
  94. exit 1
  95. }
  96. spawn telnet '"${telnetip}"' '"${telnetport}"'
  97. expect {
  98. "password:" { send "'"${telnetpass}"'\r" }
  99. default abort
  100. }
  101. expect {
  102. "session." { send "shutdown\r" }
  103. default abort
  104. }
  105. expect { eof }
  106. puts "Completed.\n"
  107. ')
  108. }
  109. fn_stop_graceful_sdtd(){
  110. fn_print_dots "Graceful: telnet"
  111. fn_script_log_info "Graceful: telnet"
  112. sleep 0.5
  113. if [ "${telnetenabled}" == "false" ]; then
  114. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  115. elif [ "$(command -v expect 2>/dev/null)" ]; then
  116. # Tries to shutdown with both localhost and server IP.
  117. for telnetip in 127.0.0.1 ${ip}; do
  118. fn_print_dots "Graceful: telnet: ${telnetip}"
  119. fn_script_log_info "Graceful: telnet: ${telnetip}"
  120. sleep 0.5
  121. fn_stop_telnet_sdtd
  122. completed=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Completed.")
  123. refused=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Timeout or EOF")
  124. if [ -n "${refused}" ]; then
  125. fn_print_error "Graceful: telnet: ${telnetip}: "
  126. fn_print_fail_eol_nl
  127. fn_script_log_error "Graceful: telnet: ${telnetip}: FAIL"
  128. sleep 1
  129. elif [ -n "${completed}" ]; then
  130. break
  131. fi
  132. done
  133. # If telnet was successful will use telnet again to check the connection has closed
  134. # This confirms that the tmux session can now be killed.
  135. if [ -n "${completed}" ]; then
  136. for seconds in {1..30}; do
  137. fn_stop_telnet_sdtd
  138. refused=$(echo -en "\n ${sdtd_telnet_shutdown}" | grep "Timeout or EOF")
  139. if [ -n "${refused}" ]; then
  140. fn_print_ok "Graceful: telnet: ${telnetip}: "
  141. fn_print_ok_eol_nl
  142. fn_script_log_pass "Graceful: telnet: ${telnetip}: ${seconds} seconds"
  143. break
  144. fi
  145. sleep 1
  146. fn_print_dots "Graceful: telnet: ${seconds}"
  147. done
  148. # If telnet failed will go straight to tmux shutdown.
  149. # If cannot shutdown correctly world save may be lost
  150. else
  151. if [ -n "${refused}" ]; then
  152. fn_print_error "Graceful: telnet: "
  153. fn_print_fail_eol_nl
  154. fn_script_log_error "Graceful: telnet: ${telnetip}: FAIL"
  155. else
  156. fn_print_error_nl "Graceful: telnet: Unknown error"
  157. fn_script_log_error "Graceful: telnet: Unknown error"
  158. fi
  159. echo -en "\n" | tee -a "${lgsmlog}"
  160. echo -en "Telnet output:" | tee -a "${lgsmlog}"
  161. echo -en "\n ${sdtd_telnet_shutdown}" | tee -a "${lgsmlog}"
  162. echo -en "\n\n" | tee -a "${lgsmlog}"
  163. fi
  164. else
  165. fn_print_warn "Graceful: telnet: expect not installed: "
  166. fn_print_fail_eol_nl
  167. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  168. fi
  169. sleep 0.5
  170. fn_stop_tmux
  171. }
  172. fn_stop_graceful_select(){
  173. if [ "${gamename}" == "7 Days To Die" ]; then
  174. fn_stop_graceful_sdtd
  175. elif [ "${engine}" == "Spark" ]; then
  176. fn_stop_graceful_cmd "q" 30
  177. elif [ "${gamename}" == "Terraria" ]; then
  178. fn_stop_graceful_cmd "exit" 30
  179. elif [ "${gamename}" == "Minecraft" ]; then
  180. fn_stop_graceful_cmd "stop" 30
  181. elif [ "${gamename}" == "Multi Theft Auto" ]; then
  182. # we need a long wait time here as resources are stopped individually and process their own shutdowns
  183. fn_stop_graceful_cmd "quit" 120
  184. elif [ "${engine}" == "goldsource" ]; then
  185. fn_stop_graceful_goldsource
  186. elif [ "${engine}" == "avalanche2.0" ]||[ "${engine}" == "avalanche3.0" ]||[ "${gamename}" == "Factorio" ]||[ "${engine}" == "unity3d" ]||[ "${engine}" == "unreal4" ]||[ "${engine}" == "unreal3" ]||[ "${engine}" == "unreal2" ]||[ "${engine}" == "unreal" ]||[ "${gamename}" == "Mumble" ]; then
  187. fn_stop_graceful_ctrlc
  188. elif [ "${engine}" == "source" ]||[ "${engine}" == "quake" ]||[ "${engine}" == "idtech2" ]||[ "${engine}" == "idtech3" ]||[ "${engine}" == "idtech3_ql" ]||[ "${engine}" == "Just Cause 2" ]||[ "${engine}" == "projectzomboid" ]||[ "${shortname}" == "rw" ]; then
  189. fn_stop_graceful_cmd "quit" 30
  190. else
  191. fn_stop_tmux
  192. fi
  193. }
  194. fn_stop_ark(){
  195. maxpiditer=15 # The maximum number of times to check if the ark pid has closed gracefully.
  196. info_config.sh
  197. if [ -z "${queryport}" ]; then
  198. fn_print_warn "No queryport found using info_config.sh"
  199. fn_script_log_warn "No queryport found using info_config.sh"
  200. userconfigfile="${serverfiles}"
  201. userconfigfile+="/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini"
  202. queryport=$(grep ^QueryPort= ${userconfigfile} | cut -d= -f2 | sed "s/[^[:digit:].*].*//g")
  203. fi
  204. if [ -z "${queryport}" ]; then
  205. fn_print_warn "No queryport found in the GameUsersettings.ini file"
  206. fn_script_log_warn "No queryport found in the GameUsersettings.ini file"
  207. return
  208. fi
  209. if [ "${#queryport}" -gt 0 ] ; then
  210. for (( pidcheck=0 ; pidcheck < ${maxpiditer} ; pidcheck++ )) ; do
  211. pid=$(netstat -nap 2>/dev/null | grep "^udp[[:space:]]" |\
  212. grep ":${queryport}[[:space:]]" | rev | awk '{print $1}' |\
  213. rev | cut -d\/ -f1)
  214. #
  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. sleep 0.5
  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. sleep 0.5
  237. "${serverfiles}"/ts3server_startscript.sh stop > /dev/null 2>&1
  238. check_status.sh
  239. if [ "${status}" == "0" ]; then
  240. # Remove lockfile
  241. rm -f "${rootdir}/${lockselfname}"
  242. fn_print_ok_nl "${servername}"
  243. fn_script_log_pass "Stopped ${servername}"
  244. else
  245. fn_print_fail_nl "Unable to stop ${servername}"
  246. fn_script_log_error "Unable to stop ${servername}"
  247. fi
  248. }
  249. fn_stop_tmux(){
  250. fn_print_dots "${servername}"
  251. fn_script_log_info "tmux kill-session: ${servername}"
  252. sleep 0.5
  253. # Kill tmux session
  254. tmux kill-session -t="${servicename}" > /dev/null 2>&1
  255. sleep 0.5
  256. check_status.sh
  257. if [ "${status}" == "0" ]; then
  258. # Remove lockfile
  259. rm -f "${rootdir}/${lockselfname}"
  260. # ARK doesn't clean up immediately after tmux is killed.
  261. # Make certain the ports are cleared before continuing.
  262. if [ "${gamename}" == "ARK: Survival Evolved" ]; then
  263. fn_stop_ark
  264. fi
  265. fn_print_ok_nl "${servername}"
  266. fn_script_log_pass "Stopped ${servername}"
  267. else
  268. fn_print_fail_nl "Unable to stop${servername}"
  269. fn_script_log_fatal "Unable to stop${servername}"
  270. fi
  271. }
  272. # checks if the server is already stopped before trying to stop.
  273. fn_stop_pre_check(){
  274. if [ "${gamename}" == "TeamSpeak 3" ]; then
  275. check_status.sh
  276. if [ "${status}" == "0" ]; then
  277. fn_print_info_nl "${servername} is already stopped"
  278. fn_script_log_error "${servername} is already stopped"
  279. else
  280. fn_stop_teamspeak3
  281. fi
  282. else
  283. if [ "${status}" == "0" ]; then
  284. fn_print_info_nl "${servername} is already stopped"
  285. fn_script_log_error "${servername} is already stopped"
  286. else
  287. fn_stop_graceful_select
  288. fi
  289. fi
  290. }
  291. fn_print_dots "${servername}"
  292. sleep 0.5
  293. check.sh
  294. info_config.sh
  295. fn_stop_pre_check
  296. core_exit.sh