command_stop.sh 10.0 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}"
  116. fn_script_log_info "Graceful: telnet: ${telnetip}"
  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}: "
  123. fn_print_fail_eol_nl
  124. fn_script_log_error "Graceful: telnet: ${telnetip}: 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}: "
  138. fn_print_ok_eol_nl
  139. fn_script_log_pass "Graceful: telnet: ${telnetip}: ${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}: 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 [ "${gamename}" == "7 Days To Die" ]; then
  170. fn_stop_graceful_sdtd
  171. elif [ "${engine}" == "Spark" ]; then
  172. fn_stop_graceful_cmd "q" 30
  173. elif [ "${gamename}" == "Terraria" ]; then
  174. fn_stop_graceful_cmd "exit" 30
  175. elif [ "${gamename}" == "Minecraft" ]; then
  176. fn_stop_graceful_cmd "stop" 30
  177. elif [ "${gamename}" == "Multi Theft Auto" ]; 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}" == "avalanche2.0" ]||[ "${engine}" == "avalanche3.0" ]||[ "${gamename}" == "Factorio" ]||[ "${engine}" == "unity3d" ]||[ "${engine}" == "unreal4" ]||[ "${engine}" == "unreal3" ]||[ "${engine}" == "unreal2" ]||[ "${engine}" == "unreal" ]||[ "${gamename}" == "Mumble" ]; then
  183. fn_stop_graceful_ctrlc
  184. elif [ "${engine}" == "source" ]||[ "${engine}" == "quake" ]||[ "${engine}" == "idtech2" ]||[ "${engine}" == "idtech3" ]||[ "${engine}" == "idtech3_ql" ]||[ "${engine}" == "Just Cause 2" ]||[ "${engine}" == "projectzomboid" ]||[ "${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:]]" |\
  206. grep ":${queryport}[[:space:]]" | rev | awk '{print $1}' |\
  207. rev | cut -d\/ -f1)
  208. #
  209. # check for a valid pid
  210. pid=${pid//[!0-9]/}
  211. let pid+=0 # turns an empty string into a valid number, '0',
  212. # and a valid numeric pid remains unchanged.
  213. if [ "${pid}" -gt 1 ]&&[ "${pid}" -le "$(cat "/proc/sys/kernel/pid_max")" ]; then
  214. fn_print_dots "Process still bound. Awaiting graceful exit: ${pidcheck}"
  215. sleep 0.5
  216. else
  217. break # Our job is done here
  218. fi # end if for pid range check
  219. done
  220. if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then
  221. # The process doesn't want to close after 20 seconds.
  222. # kill it hard.
  223. fn_print_error "Terminating reluctant Ark process: ${pid}"
  224. kill -9 ${pid}
  225. fi
  226. fi # end if for port check
  227. } # end of fn_stop_ark
  228. fn_stop_teamspeak3(){
  229. fn_print_dots "${servername}"
  230. sleep 0.5
  231. "${serverfiles}"/ts3server_startscript.sh stop > /dev/null 2>&1
  232. check_status.sh
  233. if [ "${status}" == "0" ]; then
  234. # Remove lockfile
  235. rm -f "${rootdir}/${lockselfname}"
  236. fn_print_ok_nl "${servername}"
  237. fn_script_log_pass "Stopped ${servername}"
  238. else
  239. fn_print_fail_nl "Unable to stop ${servername}"
  240. fn_script_log_error "Unable to stop ${servername}"
  241. fi
  242. }
  243. fn_stop_tmux(){
  244. fn_print_dots "${servername}"
  245. fn_script_log_info "tmux kill-session: ${servername}"
  246. sleep 0.5
  247. # Kill tmux session
  248. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  249. sleep 0.5
  250. check_status.sh
  251. if [ "${status}" == "0" ]; then
  252. # ARK doesn't clean up immediately after tmux is killed.
  253. # Make certain the ports are cleared before continuing.
  254. if [ "${gamename}" == "ARK: Survival Evolved" ]; then
  255. fn_stop_ark
  256. fi
  257. fn_print_ok_nl "${servername}"
  258. fn_script_log_pass "Stopped ${servername}"
  259. else
  260. fn_print_fail_nl "Unable to stop ${servername}"
  261. fn_script_log_fatal "Unable to stop ${servername}"
  262. fi
  263. }
  264. # checks if the server is already stopped before trying to stop.
  265. fn_stop_pre_check(){
  266. # Is the server already stopped
  267. if [ "${status}" == "0" ]; then # $status comes from check_status.sh, which is run by check.sh for this command
  268. fn_print_info_nl "${servername} is already stopped"
  269. fn_script_log_error "${servername} is already stopped"
  270. elif [ "${gamename}" == "TeamSpeak 3" ]; then
  271. fn_stop_teamspeak3
  272. else
  273. fn_stop_graceful_select
  274. fi
  275. # Check status again, a stop tmux session if needed
  276. check_status.sh
  277. if [ "${status}" != "0" ]; then
  278. fn_stop_tmux
  279. fi
  280. }
  281. fn_print_dots "${servername}"
  282. sleep 0.5
  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. core_exit.sh