command_stop.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. local 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_scriptlog "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_scriptlog "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_scriptlog "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_scriptlog "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_scriptlog "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_scriptlog "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_scriptlog "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_scriptlog "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_scriptlog "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_scriptlog "Graceful: telnet: ${telnetip}: FAIL"
  122. else
  123. fn_print_fail_nl "Graceful: telnet: Unknown error"
  124. fn_scriptlog "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_dots "Graceful: telnet: "
  133. fn_scriptlog "Graceful: telnet: "
  134. fn_print_fail "Graceful: telnet: expect not installed: "
  135. fn_print_fail_eol_nl
  136. fn_scriptlog "Graceful: telnet: expect not installed: FAIL"
  137. fi
  138. sleep 1
  139. fn_stop_tmux
  140. }
  141. fn_stop_graceful_select(){
  142. if [ "${gamename}" == "7 Days To Die" ]; then
  143. fn_stop_graceful_sdtd
  144. elif [ "${engine}" == "source" ]; then
  145. fn_stop_graceful_source
  146. elif [ "${engine}" == "goldsource" ]; then
  147. fn_stop_graceful_goldsource
  148. else
  149. fn_stop_tmux
  150. fi
  151. }
  152. fn_stop_ark(){
  153. MAXPIDITER=15 # The maximum number of times to check if the ark pid has closed gracefully.
  154. info_config.sh
  155. if [ -z $queryport ] ; then
  156. fn_print_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. 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_warn "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. fn_scriptlog "${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_scriptlog "Stopped ${servername}"
  200. else
  201. fn_print_fail_nl "Unable to stop${servername}"
  202. fn_scriptlog "Unable to stop${servername}"
  203. fi
  204. }
  205. fn_stop_tmux(){
  206. fn_print_dots "${servername}"
  207. fn_scriptlog "tmux kill-session: ${servername}"
  208. sleep 1
  209. # Kill tmux session
  210. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  211. sleep 0.5
  212. check_status.sh
  213. if [ "${status}" == "0" ]; then
  214. # Remove lock file
  215. rm -f "${rootdir}/${lockselfname}"
  216. # ARK doesn't clean up immediately after tmux is killed.
  217. # Make certain the ports are cleared before continuing.
  218. if [ "${gamename}" == "ARK: Survivial Evolved" ]; then
  219. fn_stop_ark
  220. echo -en "\n"
  221. fi
  222. fn_print_ok_nl "${servername}"
  223. fn_scriptlog "Stopped ${servername}"
  224. else
  225. fn_print_fail_nl "Unable to stop${servername}"
  226. fn_scriptlog "Unable to stop${servername}"
  227. fi
  228. }
  229. # checks if the server is already stopped before trying to stop.
  230. fn_stop_pre_check(){
  231. if [ "${gamename}" == "Teamspeak 3" ]; then
  232. check_status.sh
  233. if [ "${status}" == "0" ]; then
  234. fn_print_ok_nl "${servername} is already stopped"
  235. fn_scriptlog "${servername} is already stopped"
  236. else
  237. fn_stop_teamspeak3
  238. fi
  239. else
  240. check_status.sh
  241. if [ "${status}" == "0" ]; then
  242. fn_print_ok_nl "${servername} is already stopped"
  243. fn_scriptlog "${servername} is already stopped"
  244. else
  245. fn_stop_graceful_select
  246. fi
  247. fi
  248. }
  249. check.sh
  250. info_config.sh
  251. fn_print_dots "${servername}"
  252. fn_scriptlog "${servername}"
  253. sleep 1
  254. fn_stop_pre_check