command_stop.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. fn_stop_graceful_select(){
  141. if [ "${gamename}" == "7 Days To Die" ]; then
  142. fn_stop_graceful_sdtd
  143. elif [ "${engine}" == "source" ]; then
  144. fn_stop_graceful_source
  145. elif [ "${engine}" == "goldsource" ]; then
  146. fn_stop_graceful_goldsource
  147. else
  148. fn_stop_tmux
  149. fi
  150. }
  151. fn_stop_ark(){
  152. maxpiditer=15 # The maximum number of times to check if the ark pid has closed gracefully.
  153. info_config.sh
  154. if [ -z "${queryport}" ]; then
  155. fn_print_warn "No queryport found using info_config.sh"
  156. fn_script_log_warn "No queryport found using info_config.sh"
  157. userconfigfile="${filesdir}"
  158. userconfigfile+="/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini"
  159. queryport=$(grep ^QueryPort= ${userconfigfile} | cut -d= -f2 | sed "s/[^[:digit:].*].*//g")
  160. fi
  161. if [ -z "${queryport}" ]; then
  162. fn_print_warn "No queryport found in the GameUsersettings.ini file"
  163. fn_script_log_warn "No queryport found in the GameUsersettings.ini file"
  164. return
  165. fi
  166. if [[ ${#queryport} -gt 0 ]] ; then
  167. for (( pidcheck=0 ; pidcheck < ${maxpiditer} ; pidcheck++ )) ; do
  168. pid=$(netstat -nap 2>/dev/null | grep ^udp[[:space:]] |\
  169. grep :${queryport}[[:space:]] | rev | awk '{print $1}' |\
  170. rev | cut -d\/ -f1)
  171. #
  172. # check for a valid pid
  173. let pid+=0 # turns an empty string into a valid number, '0',
  174. # and a valid numeric pid remains unchanged.
  175. if [[ ${pid} -gt 1 && $pid -le $(cat /proc/sys/kernel/pid_max) ]] ; then
  176. fn_print_dots "Process still bound. Awaiting graceful exit: ${pidcheck}"
  177. sleep 1
  178. else
  179. break # Our job is done here
  180. fi # end if for pid range check
  181. done
  182. if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then
  183. # The process doesn't want to close after 20 seconds.
  184. # kill it hard.
  185. fn_print_error "Terminating reluctant Ark process: ${pid}"
  186. kill -9 $pid
  187. fi
  188. fi # end if for port check
  189. } # end of fn_stop_ark
  190. fn_stop_teamspeak3(){
  191. fn_print_dots "${servername}"
  192. sleep 1
  193. ${filesdir}/ts3server_startscript.sh stop > /dev/null 2>&1
  194. check_status.sh
  195. if [ "${status}" == "0" ]; then
  196. # Remove lock file
  197. rm -f "${rootdir}/${lockselfname}"
  198. fn_print_ok_nl "${servername}"
  199. fn_script_log_pass "Stopped ${servername}"
  200. else
  201. fn_print_fail_nl "Unable to stop ${servername}"
  202. fn_script_log_error "Unable to stop ${servername}"
  203. fi
  204. }
  205. fn_stop_mumble(){
  206. fn_print_dots "Stopping ${servername}"
  207. mumblepid=$(netstat -nap 2>/dev/null | grep udp | grep 64738 | grep murmur | awk '{ print $6 }' | awk -F'/' '{ print $1 }')
  208. kill ${mumblepid}
  209. sleep 1
  210. check_status.sh
  211. if [ "${status}" == "0" ]; then
  212. # Remove lock file
  213. rm -f "${rootdir}/${lockselfname}"
  214. fn_stop_tmux
  215. fn_script_log_pass "Stopped ${servername}"
  216. else
  217. fn_print_fail_nl "Unable to stop ${servername}"
  218. fn_script_log_error "Unable to stop ${servername}"
  219. fi
  220. }
  221. fn_stop_tmux(){
  222. fn_print_dots "${servername}"
  223. fn_script_log_info "tmux kill-session: ${servername}"
  224. sleep 1
  225. # Kill tmux session
  226. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  227. sleep 0.5
  228. check_status.sh
  229. if [ "${status}" == "0" ]; then
  230. # Remove lock file
  231. rm -f "${rootdir}/${lockselfname}"
  232. # ARK doesn't clean up immediately after tmux is killed.
  233. # Make certain the ports are cleared before continuing.
  234. if [ "${gamename}" == "ARK: Survivial Evolved" ]; then
  235. fn_stop_ark
  236. echo -en "\n"
  237. fi
  238. fn_print_ok_nl "${servername}"
  239. fn_script_log_pass "Stopped ${servername}"
  240. else
  241. fn_print_fail_nl "Unable to stop${servername}"
  242. fn_script_log_fatal "Unable to stop${servername}"
  243. fi
  244. }
  245. # checks if the server is already stopped before trying to stop.
  246. fn_stop_pre_check(){
  247. if [ "${gamename}" == "TeamSpeak 3" ]; then
  248. check_status.sh
  249. if [ "${status}" == "0" ]; then
  250. fn_print_info_nl "${servername} is already stopped"
  251. fn_script_log_error "${servername} is already stopped"
  252. else
  253. fn_stop_teamspeak3
  254. fi
  255. elif [ "${gamename}" == "Mumble" ]; then
  256. if [ "${status}" == "0" ]; then
  257. fn_print_info_nl "${servername} is already stopped"
  258. fn_script_log_error "${servername} is already stopped"
  259. else
  260. fn_stop_mumble
  261. fi
  262. else
  263. if [ "${status}" == "0" ]; then
  264. fn_print_info_nl "${servername} is already stopped"
  265. fn_script_log_error "${servername} is already stopped"
  266. else
  267. fn_stop_graceful_select
  268. fi
  269. fi
  270. }
  271. fn_print_dots "${servername}"
  272. sleep 1
  273. check.sh
  274. info_config.sh
  275. fn_stop_pre_check
  276. core_exit.sh