command_stop.sh 12 KB

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