command_stop.sh 7.9 KB

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