command_stop.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #!/bin/bash
  2. # LinuxGSM command_stop.sh function
  3. # Author: Daniel Gibbs
  4. # Contributors: UltimateByte
  5. # Website: https://gameservermanagers.com
  6. # Description: Stops the server.
  7. local commandname="STOP"
  8. local commandaction="Stopping"
  9. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. # Attempts graceful shutdown by sending the 'CTRL+c'.
  11. fn_stop_graceful_ctrlc(){
  12. fn_print_dots "Graceful: CTRL+c"
  13. fn_script_log_info "Graceful: CTRL+c"
  14. # sends quit
  15. tmux send-keys C-c -t "${servicename}" > /dev/null 2>&1
  16. # waits up to 30 seconds giving the server time to shutdown gracefuly
  17. for seconds in {1..30}; do
  18. check_status.sh
  19. if [ "${status}" == "0" ]; then
  20. fn_print_ok "Graceful: CTRL+c: ${seconds}: "
  21. fn_print_ok_eol_nl
  22. fn_script_log_pass "Graceful: CTRL+c: OK: ${seconds} seconds"
  23. break
  24. fi
  25. sleep 1
  26. fn_print_dots "Graceful: CTRL+c: ${seconds}"
  27. done
  28. check_status.sh
  29. if [ "${status}" != "0" ]; then
  30. fn_print_error "Graceful: CTRL+c: "
  31. fn_print_fail_eol_nl
  32. fn_script_log_error "Graceful: CTRL+c: FAIL"
  33. fi
  34. sleep 1
  35. fn_stop_tmux
  36. }
  37. # Attempts graceful shutdown by sending the 'quit' command.
  38. fn_stop_graceful_quit(){
  39. fn_print_dots "Graceful: sending \"quit\""
  40. fn_script_log_info "Graceful: sending \"quit\""
  41. # sends quit
  42. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  43. # waits up to 30 seconds giving the server time to shutdown gracefuly
  44. for seconds in {1..30}; do
  45. check_status.sh
  46. if [ "${status}" == "0" ]; then
  47. fn_print_ok "Graceful: sending \"quit\": ${seconds}: "
  48. fn_print_ok_eol_nl
  49. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  50. break
  51. fi
  52. sleep 1
  53. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  54. done
  55. check_status.sh
  56. if [ "${status}" != "0" ]; then
  57. fn_print_error "Graceful: sending \"quit\": "
  58. fn_print_fail_eol_nl
  59. fn_script_log_error "Graceful: sending \"quit\": FAIL"
  60. fi
  61. sleep 1
  62. fn_stop_tmux
  63. }
  64. # Attempts graceful of goldsource using rcon 'quit' command.
  65. # Goldsource 'quit' command restarts rather than shutdown
  66. # this function will only wait 3 seconds then force a tmux shutdown.
  67. # preventing the server from coming back online.
  68. fn_stop_graceful_goldsource(){
  69. fn_print_dots "Graceful: sending \"quit\""
  70. fn_script_log_info "Graceful: sending \"quit\""
  71. # sends quit
  72. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  73. # waits 3 seconds as goldsource servers restart with the quit command
  74. for seconds in {1..3}; do
  75. sleep 1
  76. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  77. done
  78. fn_print_ok "Graceful: sending \"quit\": ${seconds}: "
  79. fn_print_ok_eol_nl
  80. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  81. sleep 1
  82. fn_stop_tmux
  83. }
  84. # Attempts graceful of 7 Days To Die using telnet.
  85. fn_stop_telnet_sdtd(){
  86. sdtd_telnet_shutdown=$( expect -c '
  87. proc abort {} {
  88. puts "Timeout or EOF\n"
  89. exit 1
  90. }
  91. spawn telnet '"${telnetip}"' '"${telnetport}"'
  92. expect {
  93. "password:" { send "'"${telnetpass}"'\r" }
  94. default abort
  95. }
  96. expect {
  97. "session." { send "shutdown\r" }
  98. default abort
  99. }
  100. expect { eof }
  101. puts "Completed.\n"
  102. ')
  103. }
  104. fn_stop_graceful_sdtd(){
  105. fn_print_dots "Graceful: telnet"
  106. fn_script_log_info "Graceful: telnet"
  107. sleep 1
  108. if [ "${telnetenabled}" == "false" ]; then
  109. fn_print_info_nl "Graceful: telnet: DISABLED: Enable in ${servercfg}"
  110. elif [ "$(command -v expect 2>/dev/null)" ]||[ "$(which expect >/dev/null 2>&1)" ]; then
  111. # Tries to shutdown with both localhost and server IP.
  112. for telnetip in 127.0.0.1 ${ip}; do
  113. fn_print_dots "Graceful: telnet: ${telnetip}"
  114. fn_script_log_info "Graceful: telnet: ${telnetip}"
  115. sleep 1
  116. fn_stop_telnet_sdtd
  117. completed=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Completed.")
  118. refused=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Timeout or EOF")
  119. if [ -n "${refused}" ]; then
  120. fn_print_error "Graceful: telnet: ${telnetip}: "
  121. fn_print_fail_eol_nl
  122. fn_script_log_error "Graceful: telnet: ${telnetip}: FAIL"
  123. sleep 1
  124. elif [ -n "${completed}" ]; then
  125. break
  126. fi
  127. done
  128. # If telnet was successful will use telnet again to check the connection has closed
  129. # This confirms that the tmux session can now be killed.
  130. if [ -n "${completed}" ]; then
  131. for seconds in {1..30}; do
  132. fn_stop_telnet_sdtd
  133. refused=$(echo -en "\n ${sdtd_telnet_shutdown}"|grep "Timeout or EOF")
  134. if [ -n "${refused}" ]; then
  135. fn_print_ok "Graceful: telnet: ${telnetip}: "
  136. fn_print_ok_eol_nl
  137. fn_script_log_pass "Graceful: telnet: ${telnetip}: ${seconds} seconds"
  138. break
  139. fi
  140. sleep 1
  141. fn_print_dots "Graceful: telnet: ${seconds}"
  142. done
  143. # If telnet failed will go straight to tmux shutdown.
  144. # If cannot shutdown correctly world save may be lost
  145. else
  146. if [ -n "${refused}" ]; then
  147. fn_print_error "Graceful: telnet: "
  148. fn_print_fail_eol_nl
  149. fn_script_log_error "Graceful: telnet: ${telnetip}: FAIL"
  150. else
  151. fn_print_error_nl "Graceful: telnet: Unknown error"
  152. fn_script_log_error "Graceful: telnet: Unknown error"
  153. fi
  154. echo -en "\n" | tee -a "${scriptlog}"
  155. echo -en "Telnet output:" | tee -a "${scriptlog}"
  156. echo -en "\n ${sdtd_telnet_shutdown}" | tee -a "${scriptlog}"
  157. echo -en "\n\n" | tee -a "${scriptlog}"
  158. fi
  159. else
  160. fn_print_warn "Graceful: telnet: expect not installed: "
  161. fn_print_fail_eol_nl
  162. fn_script_log_warn "Graceful: telnet: expect not installed: FAIL"
  163. fi
  164. sleep 1
  165. fn_stop_tmux
  166. }
  167. # Attempts graceful of Minecraft using rcon 'stop' command.
  168. fn_stop_graceful_minecraft(){
  169. fn_print_dots "Graceful: sending \"stop\""
  170. fn_script_log_info "Graceful: sending \"stop\""
  171. # sends quit
  172. tmux send -t "${servicename}" stop ENTER > /dev/null 2>&1
  173. # waits up to 30 seconds giving the server time to shutdown gracefuly
  174. for seconds in {1..30}; do
  175. check_status.sh
  176. if [ "${status}" == "0" ]; then
  177. fn_print_ok "Graceful: sending \"stop\": ${seconds}: "
  178. fn_print_ok_eol_nl
  179. fn_script_log_pass "Graceful: sending \"stop\": OK: ${seconds} seconds"
  180. break
  181. fi
  182. sleep 1
  183. fn_print_dots "Graceful: sending \"stop\": ${seconds}"
  184. done
  185. check_status.sh
  186. if [ "${status}" != "0" ]; then
  187. fn_print_error "Graceful: sending \"stop\": "
  188. fn_print_fail_eol_nl
  189. fn_script_log_error "Graceful: sending \"stop\": FAIL"
  190. fi
  191. sleep 1
  192. fn_stop_tmux
  193. }
  194. # Attempts graceful of mta using rcon 'quit' command.
  195. fn_stop_graceful_mta(){
  196. fn_print_dots "Graceful: sending \"quit\""
  197. fn_script_log_info "Graceful: sending \"quit\""
  198. # sends quit
  199. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  200. # waits up to 120 seconds giving the server time to shutdown gracefuly, we need a long wait time here as resources are stopped individually and process their own shutdowns
  201. for seconds in {1..120}; do
  202. check_status.sh
  203. if [ "${status}" == "0" ]; then
  204. fn_print_ok "Graceful: sending \"quit\": ${seconds}: "
  205. fn_print_ok_eol_nl
  206. fn_script_log_pass "Graceful: sending \"quit\": OK: ${seconds} seconds"
  207. break
  208. fi
  209. sleep 1
  210. fn_print_dots "Graceful: sending \"quit\": ${seconds}"
  211. done
  212. check_status.sh
  213. if [ "${status}" != "0" ]; then
  214. fn_print_error "Graceful: sending \"quit\": "
  215. fn_print_fail_eol_nl
  216. fn_script_log_error "Graceful: sending \"quit\": FAIL"
  217. fi
  218. sleep 1
  219. fn_stop_tmux
  220. }
  221. # Attempts graceful of Terraria using 'exit' console command.
  222. fn_stop_graceful_terraria(){
  223. fn_print_dots "Graceful: sending \"exit\""
  224. fn_script_log_info "Graceful: sending \"exit\""
  225. # sends exit
  226. tmux send -t "${servicename}" exit ENTER > /dev/null 2>&1
  227. # waits up to 30 seconds giving the server time to shutdown gracefuly
  228. for seconds in {1..30}; do
  229. check_status.sh
  230. if [ "${status}" == "0" ]; then
  231. fn_print_ok "Graceful: sending \"exit\": ${seconds}: "
  232. fn_print_ok_eol_nl
  233. fn_script_log_pass "Graceful: sending \"exit\": OK: ${seconds} seconds"
  234. break
  235. fi
  236. sleep 1
  237. fn_print_dots "Graceful: sending \"exit\": ${seconds}"
  238. done
  239. check_status.sh
  240. if [ "${status}" != "0" ]; then
  241. fn_print_error "Graceful: sending \"exit\": "
  242. fn_print_fail_eol_nl
  243. fn_script_log_error "Graceful: sending \"exit\": FAIL"
  244. fi
  245. sleep 1
  246. fn_stop_tmux
  247. }
  248. fn_stop_graceful_select(){
  249. if [ "${gamename}" == "7 Days To Die" ]; then
  250. fn_stop_graceful_sdtd
  251. elif [ "${gamename}" == "Terraria" ]; then
  252. fn_stop_graceful_terraria
  253. elif [ "${gamename}" == "Minecraft" ]; then
  254. fn_stop_graceful_minecraft
  255. elif [ "${gamename}" == "Multi Theft Auto" ]; then
  256. fn_stop_graceful_mta
  257. elif [ "${engine}" == "goldsource" ]; then
  258. fn_stop_graceful_goldsource
  259. elif [ "${gamename}" == "Factorio" ]||[ "${engine}" == "unity3d" ]||[ "${engine}" == "unreal4" ]||[ "${engine}" == "unreal3" ]||[ "${engine}" == "unreal2" ]||[ "${engine}" == "unreal" ]||[ "${gamename}" == "Mumble" ]; then
  260. fn_stop_graceful_ctrlc
  261. elif [ "${engine}" == "source" ]||[ "${engine}" == "quake" ]||[ "${engine}" == "idtech2" ]||[ "${engine}" == "idtech3" ]||[ "${engine}" == "idtech3_ql" ]||[ "${engine}" == "Just Cause 2" ]; then
  262. fn_stop_graceful_quit
  263. else
  264. fn_stop_tmux
  265. fi
  266. }
  267. fn_stop_ark(){
  268. maxpiditer=15 # The maximum number of times to check if the ark pid has closed gracefully.
  269. info_config.sh
  270. if [ -z "${queryport}" ]; then
  271. fn_print_warn "No queryport found using info_config.sh"
  272. fn_script_log_warn "No queryport found using info_config.sh"
  273. userconfigfile="${filesdir}"
  274. userconfigfile+="/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini"
  275. queryport=$(grep ^QueryPort= ${userconfigfile} | cut -d= -f2 | sed "s/[^[:digit:].*].*//g")
  276. fi
  277. if [ -z "${queryport}" ]; then
  278. fn_print_warn "No queryport found in the GameUsersettings.ini file"
  279. fn_script_log_warn "No queryport found in the GameUsersettings.ini file"
  280. return
  281. fi
  282. if [ "${#queryport}" -gt 0 ] ; then
  283. for (( pidcheck=0 ; pidcheck < ${maxpiditer} ; pidcheck++ )) ; do
  284. pid=$(netstat -nap 2>/dev/null | grep ^udp[[:space:]] |\
  285. grep :${queryport}[[:space:]] | rev | awk '{print $1}' |\
  286. rev | cut -d\/ -f1)
  287. #
  288. # check for a valid pid
  289. pid=${pid//[!0-9]/}
  290. let pid+=0 # turns an empty string into a valid number, '0',
  291. # and a valid numeric pid remains unchanged.
  292. if [ "${pid}" -gt 1 ]&&[ "${pid}" -le $(cat /proc/sys/kernel/pid_max) ]; then
  293. fn_print_dots "Process still bound. Awaiting graceful exit: ${pidcheck}"
  294. sleep 1
  295. else
  296. break # Our job is done here
  297. fi # end if for pid range check
  298. done
  299. if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then
  300. # The process doesn't want to close after 20 seconds.
  301. # kill it hard.
  302. fn_print_error "Terminating reluctant Ark process: ${pid}"
  303. kill -9 ${pid}
  304. fi
  305. fi # end if for port check
  306. } # end of fn_stop_ark
  307. fn_stop_teamspeak3(){
  308. fn_print_dots "${servername}"
  309. sleep 0.5
  310. "${filesdir}"/ts3server_startscript.sh stop > /dev/null 2>&1
  311. check_status.sh
  312. if [ "${status}" == "0" ]; then
  313. # Remove lockfile
  314. rm -f "${rootdir}/${lockselfname}"
  315. fn_print_ok_nl "${servername}"
  316. fn_script_log_pass "Stopped ${servername}"
  317. else
  318. fn_print_fail_nl "Unable to stop ${servername}"
  319. fn_script_log_error "Unable to stop ${servername}"
  320. fi
  321. }
  322. fn_stop_tmux(){
  323. fn_print_dots "${servername}"
  324. fn_script_log_info "tmux kill-session: ${servername}"
  325. sleep 0.5
  326. # Kill tmux session
  327. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  328. sleep 0.5
  329. check_status.sh
  330. if [ "${status}" == "0" ]; then
  331. # Remove lockfile
  332. rm -f "${rootdir}/${lockselfname}"
  333. # ARK doesn't clean up immediately after tmux is killed.
  334. # Make certain the ports are cleared before continuing.
  335. if [ "${gamename}" == "ARK: Survival Evolved" ]; then
  336. fn_stop_ark
  337. fi
  338. fn_print_ok_nl "${servername}"
  339. fn_script_log_pass "Stopped ${servername}"
  340. else
  341. fn_print_fail_nl "Unable to stop${servername}"
  342. fn_script_log_fatal "Unable to stop${servername}"
  343. fi
  344. }
  345. # checks if the server is already stopped before trying to stop.
  346. fn_stop_pre_check(){
  347. if [ "${gamename}" == "TeamSpeak 3" ]; then
  348. check_status.sh
  349. if [ "${status}" == "0" ]; then
  350. fn_print_info_nl "${servername} is already stopped"
  351. fn_script_log_error "${servername} is already stopped"
  352. else
  353. fn_stop_teamspeak3
  354. fi
  355. else
  356. if [ "${status}" == "0" ]; then
  357. fn_print_info_nl "${servername} is already stopped"
  358. fn_script_log_error "${servername} is already stopped"
  359. else
  360. fn_stop_graceful_select
  361. fi
  362. fi
  363. }
  364. fn_print_dots "${servername}"
  365. sleep 0.5
  366. check.sh
  367. info_config.sh
  368. fn_stop_pre_check
  369. core_exit.sh