command_stop.sh 8.4 KB

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