command_stop.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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_factorio(){
  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. fn_stop_graceful_select(){
  184. if [ "${gamename}" == "7 Days To Die" ]; then
  185. fn_stop_graceful_sdtd
  186. elif [ "${gamename}" == "Factorio" ]; then
  187. fn_stop_graceful_factorio
  188. elif [ "${engine}" == "source" ]; then
  189. fn_stop_graceful_source
  190. elif [ "${engine}" == "goldsource" ]; then
  191. fn_stop_graceful_goldsource
  192. elif [ "${engine}" == "lwjgl2" ]; then
  193. fn_stop_graceful_minecraft
  194. else
  195. fn_stop_tmux
  196. fi
  197. }
  198. fn_stop_ark(){
  199. maxpiditer=15 # The maximum number of times to check if the ark pid has closed gracefully.
  200. info_config.sh
  201. if [ -z "${queryport}" ]; then
  202. fn_print_warn "No queryport found using info_config.sh"
  203. fn_script_log_warn "No queryport found using info_config.sh"
  204. userconfigfile="${filesdir}"
  205. userconfigfile+="/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini"
  206. queryport=$(grep ^QueryPort= ${userconfigfile} | cut -d= -f2 | sed "s/[^[:digit:].*].*//g")
  207. fi
  208. if [ -z "${queryport}" ]; then
  209. fn_print_warn "No queryport found in the GameUsersettings.ini file"
  210. fn_script_log_warn "No queryport found in the GameUsersettings.ini file"
  211. return
  212. fi
  213. if [[ ${#queryport} -gt 0 ]] ; then
  214. for (( pidcheck=0 ; pidcheck < ${maxpiditer} ; pidcheck++ )) ; do
  215. pid=$(netstat -nap 2>/dev/null | grep ^udp[[:space:]] |\
  216. grep :${queryport}[[:space:]] | rev | awk '{print $1}' |\
  217. rev | cut -d\/ -f1)
  218. #
  219. # check for a valid pid
  220. pid=${pid//[!0-9]/}
  221. let pid+=0 # turns an empty string into a valid number, '0',
  222. # and a valid numeric pid remains unchanged.
  223. if [[ ${pid} -gt 1 && $pid -le $(cat /proc/sys/kernel/pid_max) ]] ; then
  224. fn_print_dots "Process still bound. Awaiting graceful exit: ${pidcheck}"
  225. sleep 1
  226. else
  227. break # Our job is done here
  228. fi # end if for pid range check
  229. done
  230. if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then
  231. # The process doesn't want to close after 20 seconds.
  232. # kill it hard.
  233. fn_print_error "Terminating reluctant Ark process: ${pid}"
  234. kill -9 $pid
  235. fi
  236. fi # end if for port check
  237. } # end of fn_stop_ark
  238. fn_stop_teamspeak3(){
  239. fn_print_dots "${servername}"
  240. sleep 1
  241. ${filesdir}/ts3server_startscript.sh stop > /dev/null 2>&1
  242. check_status.sh
  243. if [ "${status}" == "0" ]; then
  244. # Remove lockfile
  245. rm -f "${rootdir}/${lockselfname}"
  246. fn_print_ok_nl "${servername}"
  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_mumble(){
  254. # Get needed port info
  255. info_config.sh
  256. fn_print_dots "Stopping ${servername}"
  257. mumblepid=$(netstat -nap 2>/dev/null | grep udp | grep "${port}" | grep murmur | awk '{ print $6 }' | awk -F'/' '{ print $1 }')
  258. kill ${mumblepid}
  259. sleep 1
  260. check_status.sh
  261. if [ "${status}" == "0" ]; then
  262. # Remove lockfile
  263. rm -f "${rootdir}/${lockselfname}"
  264. fn_stop_tmux
  265. fn_script_log_pass "Stopped ${servername}"
  266. else
  267. fn_print_fail_nl "Unable to stop ${servername}"
  268. fn_script_log_error "Unable to stop ${servername}"
  269. fi
  270. }
  271. fn_stop_tmux(){
  272. fn_print_dots "${servername}"
  273. fn_script_log_info "tmux kill-session: ${servername}"
  274. sleep 1
  275. # Kill tmux session
  276. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  277. sleep 0.5
  278. check_status.sh
  279. if [ "${status}" == "0" ]; then
  280. # Remove lockfile
  281. rm -f "${rootdir}/${lockselfname}"
  282. # ARK doesn't clean up immediately after tmux is killed.
  283. # Make certain the ports are cleared before continuing.
  284. if [ "${gamename}" == "ARK: Survival Evolved" ]; then
  285. fn_stop_ark
  286. echo -en "\n"
  287. fi
  288. fn_print_ok_nl "${servername}"
  289. fn_script_log_pass "Stopped ${servername}"
  290. else
  291. fn_print_fail_nl "Unable to stop${servername}"
  292. fn_script_log_fatal "Unable to stop${servername}"
  293. fi
  294. }
  295. # checks if the server is already stopped before trying to stop.
  296. fn_stop_pre_check(){
  297. if [ "${gamename}" == "TeamSpeak 3" ]; then
  298. check_status.sh
  299. if [ "${status}" == "0" ]; then
  300. fn_print_info_nl "${servername} is already stopped"
  301. fn_script_log_error "${servername} is already stopped"
  302. else
  303. fn_stop_teamspeak3
  304. fi
  305. elif [ "${gamename}" == "Mumble" ]; then
  306. if [ "${status}" == "0" ]; then
  307. fn_print_info_nl "${servername} is already stopped"
  308. fn_script_log_error "${servername} is already stopped"
  309. else
  310. fn_stop_mumble
  311. fi
  312. else
  313. if [ "${status}" == "0" ]; then
  314. fn_print_info_nl "${servername} is already stopped"
  315. fn_script_log_error "${servername} is already stopped"
  316. else
  317. fn_stop_graceful_select
  318. fi
  319. fi
  320. }
  321. fn_print_dots "${servername}"
  322. sleep 1
  323. check.sh
  324. info_config.sh
  325. fn_stop_pre_check
  326. core_exit.sh