command_stop.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #!/bin/bash
  2. # LinuxGSM command_stop.sh function
  3. # Author: Daniel Gibbs
  4. # Contributors: UltimateByte
  5. # Website: https://gameservermanagers.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 1
  35. fn_stop_tmux
  36. }
  37. # Attempts graceful shutdown by sending the 'quit' command.
  38. fn_stop_graceful_quit(){
  39. fn_print_dots "Graceful: sending \"quit\""
  40. fn_script_log_info "Graceful: sending \"quit\""
  41. # sends quit
  42. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  43. # waits up to 30 seconds giving the server time to shutdown gracefuly
  44. for seconds in {1..30}; do
  45. check_status.sh
  46. if [ "${status}" == "0" ]; then
  47. fn_print_ok "Graceful: sending \"quit\": ${seconds}: "
  48. fn_print_ok_eol_nl
  49. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  50. break
  51. fi
  52. sleep 1
  53. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  54. done
  55. check_status.sh
  56. if [ "${status}" != "0" ]; then
  57. fn_print_error "Graceful: sending \"quit\": "
  58. fn_print_fail_eol_nl
  59. fn_script_log_error "Graceful: sending \"quit\": FAIL"
  60. fi
  61. sleep 1
  62. fn_stop_tmux
  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 1
  82. fn_stop_tmux
  83. }
  84. # Attempts graceful of 7 Days To Die using telnet.
  85. fn_stop_telnet_sdtd(){
  86. if [ -z "${telnetpass}"]; then
  87. telnetpass="NOTSET"
  88. fi
  89. sdtd_telnet_shutdown=$( expect -c '
  90. proc abort {} {
  91. puts "Timeout or EOF\n"
  92. exit 1
  93. }
  94. spawn telnet '"${telnetip}"' '"${telnetport}"'
  95. expect {
  96. "password:" { send "'"${telnetpass}"'\r" }
  97. default abort
  98. }
  99. expect {
  100. "session." { send "shutdown\r" }
  101. default abort
  102. }
  103. expect { eof }
  104. puts "Completed.\n"
  105. ')
  106. }
  107. fn_stop_graceful_sdtd(){
  108. fn_print_dots "Graceful: telnet"
  109. fn_script_log_info "Graceful: telnet"
  110. sleep 1
  111. if [ "${telnetenabled}" == "false" ]; then
  112. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  113. elif [ "$(command -v expect 2>/dev/null)" ]||[ "$(which expect >/dev/null 2>&1)" ]; then
  114. # Tries to shutdown with both localhost and server IP.
  115. for telnetip in 127.0.0.1 ${ip}; do
  116. fn_print_dots "Graceful: telnet: ${telnetip}"
  117. fn_script_log_info "Graceful: telnet: ${telnetip}"
  118. sleep 1
  119. fn_stop_telnet_sdtd
  120. completed=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Completed.")
  121. refused=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Timeout or EOF")
  122. if [ -n "${refused}" ]; then
  123. fn_print_error "Graceful: telnet: ${telnetip}: "
  124. fn_print_fail_eol_nl
  125. fn_script_log_error "Graceful: telnet: ${telnetip}: FAIL"
  126. sleep 1
  127. elif [ -n "${completed}" ]; then
  128. break
  129. fi
  130. done
  131. # If telnet was successful will use telnet again to check the connection has closed
  132. # This confirms that the tmux session can now be killed.
  133. if [ -n "${completed}" ]; then
  134. for seconds in {1..30}; do
  135. fn_stop_telnet_sdtd
  136. refused=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Timeout or EOF")
  137. if [ -n "${refused}" ]; then
  138. fn_print_ok "Graceful: telnet: ${telnetip}: "
  139. fn_print_ok_eol_nl
  140. fn_script_log_pass "Graceful: telnet: ${telnetip}: ${seconds} seconds"
  141. break
  142. fi
  143. sleep 1
  144. fn_print_dots "Graceful: telnet: ${seconds}"
  145. done
  146. # If telnet failed will go straight to tmux shutdown.
  147. # If cannot shutdown correctly world save may be lost
  148. else
  149. if [ -n "${refused}" ]; then
  150. fn_print_error "Graceful: telnet: "
  151. fn_print_fail_eol_nl
  152. fn_script_log_error "Graceful: telnet: ${telnetip}: FAIL"
  153. else
  154. fn_print_error_nl "Graceful: telnet: Unknown error"
  155. fn_script_log_error "Graceful: telnet: Unknown error"
  156. fi
  157. echo -en "\n" | tee -a "${lgsmlog}"
  158. echo -en "Telnet output:" | tee -a "${lgsmlog}"
  159. echo -en "\n ${sdtd_telnet_shutdown}" | tee -a "${lgsmlog}"
  160. echo -en "\n\n" | tee -a "${lgsmlog}"
  161. fi
  162. else
  163. fn_print_warn "Graceful: telnet: expect not installed: "
  164. fn_print_fail_eol_nl
  165. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  166. fi
  167. sleep 1
  168. fn_stop_tmux
  169. }
  170. # Attempts graceful of source using rcon 'stop' command.
  171. fn_stop_graceful_minecraft(){
  172. fn_print_dots "Graceful: sending \"stop\""
  173. fn_script_log_info "Graceful: sending \"stop\""
  174. # sends quit
  175. tmux send -t "${servicename}" stop ENTER > /dev/null 2>&1
  176. # waits up to 30 seconds giving the server time to shutdown gracefuly
  177. for seconds in {1..30}; do
  178. check_status.sh
  179. if [ "${status}" == "0" ]; then
  180. fn_print_ok "Graceful: sending \"stop\": ${seconds}: "
  181. fn_print_ok_eol_nl
  182. fn_script_log_pass "Graceful: sending \"stop\": OK: ${seconds} seconds"
  183. break
  184. fi
  185. sleep 1
  186. fn_print_dots "Graceful: sending \"stop\": ${seconds}"
  187. done
  188. check_status.sh
  189. if [ "${status}" != "0" ]; then
  190. fn_print_error "Graceful: sending \"stop\": "
  191. fn_print_fail_eol_nl
  192. fn_script_log_error "Graceful: sending \"stop\": FAIL"
  193. fi
  194. sleep 1
  195. fn_stop_tmux
  196. }
  197. # Attempts graceful of mta using rcon 'quit' command.
  198. fn_stop_graceful_mta(){
  199. fn_print_dots "Graceful: sending \"quit\""
  200. fn_script_log_info "Graceful: sending \"quit\""
  201. # sends quit
  202. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  203. # waits up to 120 seconds giving the server time to shutdown gracefuly, we need a long wait time here as resources are stopped individually and process their own shutdowns
  204. for seconds in {1..120}; do
  205. check_status.sh
  206. if [ "${status}" == "0" ]; then
  207. fn_print_ok "Graceful: sending \"quit\": ${seconds}: "
  208. fn_print_ok_eol_nl
  209. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  210. break
  211. fi
  212. sleep 1
  213. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  214. done
  215. check_status.sh
  216. if [ "${status}" != "0" ]; then
  217. fn_print_error "Graceful: sending \"quit\": "
  218. fn_print_fail_eol_nl
  219. fn_script_log_error "Graceful: sending \"quit\": FAIL"
  220. fi
  221. sleep 1
  222. fn_stop_tmux
  223. }
  224. fn_stop_graceful_select(){
  225. if [ "${gamename}" == "7 Days To Die" ]; then
  226. fn_stop_graceful_sdtd
  227. elif [ "${gamename}" == "Factorio" ]||[ "${gamename}" == "Minecraft" ]||[ "${gamename}" == "Multi Theft Auto" ]||[ "${engine}" == "unity3d" ]||[ "${engine}" == "unreal4" ]||[ "${engine}" == "unreal3" ]||[ "${engine}" == "unreal2" ]||[ "${engine}" == "unreal" ]||[ "${gamename}" == "Mumble" ]; then
  228. fn_stop_graceful_ctrlc
  229. elif [ "${engine}" == "source" ]||[ "${engine}" == "quake" ]||[ "${engine}" == "idtech2" ]||[ "${engine}" == "idtech3" ]||[ "${engine}" == "idtech3_ql" ]||[ "${engine}" == "Just Cause 2" ]; then
  230. fn_stop_graceful_quit
  231. elif [ "${engine}" == "goldsource" ]; then
  232. fn_stop_graceful_goldsource
  233. elif [ "${engine}" == "lwjgl2" ]; then
  234. fn_stop_graceful_minecraft
  235. elif [ "${engine}" == "renderware" ]; then
  236. fn_stop_graceful_mta
  237. else
  238. fn_stop_tmux
  239. fi
  240. }
  241. fn_stop_ark(){
  242. maxpiditer=15 # The maximum number of times to check if the ark pid has closed gracefully.
  243. info_config.sh
  244. if [ -z "${queryport}" ]; then
  245. fn_print_warn "No queryport found using info_config.sh"
  246. fn_script_log_warn "No queryport found using info_config.sh"
  247. userconfigfile="${serverfiles}"
  248. userconfigfile+="/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini"
  249. queryport=$(grep ^QueryPort= ${userconfigfile} | cut -d= -f2 | sed "s/[^[:digit:].*].*//g")
  250. fi
  251. if [ -z "${queryport}" ]; then
  252. fn_print_warn "No queryport found in the GameUsersettings.ini file"
  253. fn_script_log_warn "No queryport found in the GameUsersettings.ini file"
  254. return
  255. fi
  256. if [ "${#queryport}" -gt 0 ] ; then
  257. for (( pidcheck=0 ; pidcheck < ${maxpiditer} ; pidcheck++ )) ; do
  258. pid=$(netstat -nap 2>/dev/null | grep ^udp[[:space:]] |\
  259. grep :${queryport}[[:space:]] | rev | awk '{print $1}' |\
  260. rev | cut -d\/ -f1)
  261. #
  262. # check for a valid pid
  263. pid=${pid//[!0-9]/}
  264. let pid+=0 # turns an empty string into a valid number, '0',
  265. # and a valid numeric pid remains unchanged.
  266. if [ "${pid}" -gt 1 ]&&[ "${pid}" -le $(cat /proc/sys/kernel/pid_max) ]; then
  267. fn_print_dots "Process still bound. Awaiting graceful exit: ${pidcheck}"
  268. sleep 1
  269. else
  270. break # Our job is done here
  271. fi # end if for pid range check
  272. done
  273. if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then
  274. # The process doesn't want to close after 20 seconds.
  275. # kill it hard.
  276. fn_print_error "Terminating reluctant Ark process: ${pid}"
  277. kill -9 ${pid}
  278. fi
  279. fi # end if for port check
  280. } # end of fn_stop_ark
  281. fn_stop_teamspeak3(){
  282. fn_print_dots "${servername}"
  283. sleep 0.5
  284. "${serverfiles}"/ts3server_startscript.sh stop > /dev/null 2>&1
  285. check_status.sh
  286. if [ "${status}" == "0" ]; then
  287. # Remove lockfile
  288. rm -f "${rootdir}/${lockselfname}"
  289. fn_print_ok_nl "${servername}"
  290. fn_script_log_pass "Stopped ${servername}"
  291. else
  292. fn_print_fail_nl "Unable to stop ${servername}"
  293. fn_script_log_error "Unable to stop ${servername}"
  294. fi
  295. }
  296. fn_stop_tmux(){
  297. fn_print_dots "${servername}"
  298. fn_script_log_info "tmux kill-session: ${servername}"
  299. sleep 0.5
  300. # Kill tmux session
  301. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  302. sleep 0.5
  303. check_status.sh
  304. if [ "${status}" == "0" ]; then
  305. # Remove lockfile
  306. rm -f "${rootdir}/${lockselfname}"
  307. # ARK doesn't clean up immediately after tmux is killed.
  308. # Make certain the ports are cleared before continuing.
  309. if [ "${gamename}" == "ARK: Survival Evolved" ]; then
  310. fn_stop_ark
  311. fi
  312. fn_print_ok_nl "${servername}"
  313. fn_script_log_pass "Stopped ${servername}"
  314. else
  315. fn_print_fail_nl "Unable to stop${servername}"
  316. fn_script_log_fatal "Unable to stop${servername}"
  317. fi
  318. }
  319. # checks if the server is already stopped before trying to stop.
  320. fn_stop_pre_check(){
  321. if [ "${gamename}" == "TeamSpeak 3" ]; then
  322. check_status.sh
  323. if [ "${status}" == "0" ]; then
  324. fn_print_info_nl "${servername} is already stopped"
  325. fn_script_log_error "${servername} is already stopped"
  326. else
  327. fn_stop_teamspeak3
  328. fi
  329. else
  330. if [ "${status}" == "0" ]; then
  331. fn_print_info_nl "${servername} is already stopped"
  332. fn_script_log_error "${servername} is already stopped"
  333. else
  334. fn_stop_graceful_select
  335. fi
  336. fi
  337. }
  338. fn_print_dots "${servername}"
  339. sleep 0.5
  340. check.sh
  341. info_config.sh
  342. fn_stop_pre_check
  343. core_exit.sh