4
0

command_stop.sh 9.7 KB

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