4
0

command_stop.sh 11 KB

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