command_stop.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/bin/bash
  2. # LGSM command_stop.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://gameservermanagers.com
  5. lgsm_version="050616"
  6. # Description: Stops the server.
  7. modulename="Stopping"
  8. 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_fail "Graceful: rcon quit: "
  30. fn_print_fail_eol_nl
  31. fn_script_log_fail "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_warn "Graceful: telnet: ${telnetip}: "
  93. fn_print_fail_eol_nl
  94. fn_script_log_warn "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_fail "Graceful: telnet: "
  120. fn_print_fail_eol_nl
  121. fn_script_log_fail "Graceful: telnet: ${telnetip}: FAIL"
  122. else
  123. fn_print_fail_nl "Graceful: telnet: Unknown error"
  124. fn_script_log_fail "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_fail "Graceful: telnet: expect not installed: "
  133. fn_print_fail_eol_nl
  134. fn_script_log_fail "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. userconfigfile="${filesdir}"
  156. userconfigfile+="/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini"
  157. queryport=$(grep ^QueryPort= ${userconfigfile} | cut -d= -f2 | sed "s/[^[:digit:].*].*//g")
  158. fi
  159. if [ -z "${queryport}" ]; then
  160. fn_print_warn "no queryport found in the GameUsersettings.ini file"
  161. return
  162. fi
  163. if [[ ${#queryport} -gt 0 ]] ; then
  164. for (( pidcheck=0 ; pidcheck < ${maxpiditer} ; pidcheck++ )) ; do
  165. pid=$(netstat -nap 2>/dev/null | grep ^udp[[:space:]] |\
  166. grep :${queryport}[[:space:]] | rev | awk '{print $1}' |\
  167. rev | cut -d\/ -f1)
  168. #
  169. # check for a valid pid
  170. let pid+=0 # turns an empty string into a valid number, '0',
  171. # and a valid numeric pid remains unchanged.
  172. if [[ $pid -gt 1 && $pid -le $(cat /proc/sys/kernel/pid_max) ]] ; then
  173. fn_print_dots "Process still bound. Awaiting graceful exit: $pidcheck"
  174. sleep 1
  175. else
  176. break # Our job is done here
  177. fi # end if for pid range check
  178. done
  179. if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then
  180. # The process doesn't want to close after 20 seconds.
  181. # kill it hard.
  182. fn_print_warn "Terminating reluctant Ark process: $pid"
  183. kill -9 $pid
  184. fi
  185. fi # end if for port check
  186. } # end of fn_stop_ark
  187. fn_stop_teamspeak3(){
  188. fn_print_dots "${servername}"
  189. sleep 1
  190. ${filesdir}/ts3server_startscript.sh stop > /dev/null 2>&1
  191. check_status.sh
  192. if [ "${status}" == "0" ]; then
  193. # Remove lock file
  194. rm -f "${rootdir}/${lockselfname}"
  195. fn_print_ok_nl "${servername}"
  196. fn_script_log_pass "Stopped ${servername}"
  197. else
  198. fn_print_fail_nl "Unable to stop${servername}"
  199. fn_script_log_fail "Unable to stop${servername}"
  200. fi
  201. }
  202. fn_stop_tmux(){
  203. fn_print_dots "${servername}"
  204. fn_script_log_info "tmux kill-session: ${servername}"
  205. sleep 1
  206. # Kill tmux session
  207. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  208. sleep 0.5
  209. check_status.sh
  210. if [ "${status}" == "0" ]; then
  211. # Remove lock file
  212. rm -f "${rootdir}/${lockselfname}"
  213. # ARK doesn't clean up immediately after tmux is killed.
  214. # Make certain the ports are cleared before continuing.
  215. if [ "${gamename}" == "ARK: Survivial Evolved" ]; then
  216. fn_stop_ark
  217. echo -en "\n"
  218. fi
  219. fn_print_ok_nl "${servername}"
  220. fn_script_log_pass "Stopped ${servername}"
  221. else
  222. fn_print_fail_nl "Unable to stop${servername}"
  223. fn_script_log_error "Unable to stop${servername}"
  224. fi
  225. }
  226. # checks if the server is already stopped before trying to stop.
  227. fn_stop_pre_check(){
  228. if [ "${gamename}" == "Teamspeak 3" ]; then
  229. check_status.sh
  230. if [ "${status}" == "0" ]; then
  231. fn_print_ok_nl "${servername} is already stopped"
  232. fn_script_log "${servername} is already stopped"
  233. else
  234. fn_stop_teamspeak3
  235. fi
  236. else
  237. check_status.sh
  238. if [ "${status}" == "0" ]; then
  239. fn_print_ok_nl "${servername} is already stopped"
  240. fn_script_log "${servername} is already stopped"
  241. else
  242. fn_stop_graceful_select
  243. fi
  244. fi
  245. }
  246. check.sh
  247. info_config.sh
  248. fn_print_dots "${servername}"
  249. fn_script_log "${servername}"
  250. sleep 1
  251. fn_stop_pre_check
  252. core_exit.sh