command_stop.sh 8.8 KB

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