From 0fec527800ad0fd1d93928f07b4347cc4bcd0c1c Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Sat, 2 Mar 2019 22:35:03 +0000 Subject: [PATCH] fix(stop): fix if statements and refactor comments --- lgsm/functions/command_stop.sh | 65 ++++++++++++++++------------------ 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/lgsm/functions/command_stop.sh b/lgsm/functions/command_stop.sh index fc43476e6..77d0e0f60 100644 --- a/lgsm/functions/command_stop.sh +++ b/lgsm/functions/command_stop.sh @@ -9,13 +9,13 @@ local commandname="STOP" local commandaction="Stopping" local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" -# Attempts graceful shutdown by sending the 'CTRL+c'. +# Attempts graceful shutdown by sending 'CTRL+c'. fn_stop_graceful_ctrlc(){ fn_print_dots "Graceful: CTRL+c" fn_script_log_info "Graceful: CTRL+c" - # sends quit + # Sends quit. tmux send-keys -t "${servicename}" C-c > /dev/null 2>&1 - # waits up to 30 seconds giving the server time to shutdown gracefuly + # Waits up to 30 seconds giving the server time to shutdown gracefuly. for seconds in {1..30}; do check_status.sh if [ "${status}" == "0" ]; then @@ -38,13 +38,13 @@ fn_stop_graceful_ctrlc(){ # Attempts graceful shutdown by sending a specified command. # Usage: fn_stop_graceful_cmd "console_command" "timeout_in_seconds" -# e.g.: fn_stop_graceful_cmd "quit" "30" +# e.g.: fn_stop_graceful_cmd "quit" "30" fn_stop_graceful_cmd(){ fn_print_dots "Graceful: sending \"${1}\"" fn_script_log_info "Graceful: sending \"${1}\"" - # sends specific stop command + # Sends specific stop command. tmux send -t "${servicename}" "${1}" ENTER > /dev/null 2>&1 - # waits up to given seconds giving the server time to shutdown gracefully + # Waits up to ${seconds} seconds giving the server time to shutdown gracefully. for ((seconds=1; seconds<=${2}; seconds++)); do check_status.sh if [ "${status}" == "0" ]; then @@ -65,16 +65,15 @@ fn_stop_graceful_cmd(){ sleep 0.5 } -# Attempts graceful of goldsource using rcon 'quit' command. -# Goldsource 'quit' command restarts rather than shutdown -# this function will only wait 3 seconds then force a tmux shutdown. -# preventing the server from coming back online. +# Attempts graceful shutdown of goldsource using rcon 'quit' command. +# There is only a 3 second delay before a forced a tmux shutdown +# as Goldsource servers 'quit' command does a restart rather than shutdown. fn_stop_graceful_goldsource(){ fn_print_dots "Graceful: sending \"quit\"" fn_script_log_info "Graceful: sending \"quit\"" # sends quit tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1 - # waits 3 seconds as goldsource servers restart with the quit command + # Waits 3 seconds as goldsource servers restart with the quit command. for seconds in {1..3}; do sleep 1 fn_print_dots "Graceful: sending \"quit\": ${seconds}" @@ -85,7 +84,7 @@ fn_stop_graceful_goldsource(){ sleep 0.5 } -# Attempts graceful of 7 Days To Die using telnet. + fn_stop_telnet_sdtd(){ if [ -z "${telnetpass}" ]||[ "${telnetpass}" == "NOT SET" ]; then sdtd_telnet_shutdown=$( expect -c ' @@ -122,6 +121,7 @@ fn_stop_telnet_sdtd(){ fi } +# Attempts graceful shutdown of 7 Days To Die using telnet. fn_stop_graceful_sdtd(){ fn_print_dots "Graceful: telnet" fn_script_log_info "Graceful: telnet" @@ -147,8 +147,8 @@ fn_stop_graceful_sdtd(){ fi done - # If telnet was successful will use telnet again to check the connection has closed - # This confirms that the tmux session can now be killed. + # If telnet shutdown was successful will use telnet again to check + # the connection has closed, confirming that the tmux session can now be killed. if [ -n "${completed}" ]; then for seconds in {1..30}; do fn_stop_telnet_sdtd @@ -162,8 +162,7 @@ fn_stop_graceful_sdtd(){ sleep 1 fn_print_dots "Graceful: telnet: ${seconds}" done - # If telnet failed will go straight to tmux shutdown. - # If cannot shutdown correctly world save may be lost + # If telnet shutdown fails tmux shutdown will be used, this risks loss of world save. else if [ -n "${refused}" ]; then fn_print_error "Graceful: telnet: " @@ -189,26 +188,28 @@ fn_stop_graceful_sdtd(){ fn_stop_graceful_select(){ if [ "${shortname}" == "sdtd" ]; then fn_stop_graceful_sdtd - elif [ "${engine}" == "Spark" ]; then + elif [ "${engine}" == "spark" ]; then fn_stop_graceful_cmd "q" 30 elif [ "${shortname}" == "terraria" ]; then fn_stop_graceful_cmd "exit" 30 elif [ "${shortname}" == "mc" ]; then fn_stop_graceful_cmd "stop" 30 elif [ "${shortname}" == "mta" ]; then - # we need a long wait time here as resources are stopped individually and process their own shutdowns + # long wait time required for mta + # as resources shutdown individually fn_stop_graceful_cmd "quit" 120 elif [ "${engine}" == "goldsource" ]; then fn_stop_graceful_goldsource elif [ "${engine}" == "unity3d" ]||[ "${engine}" == "unreal4" ]||[ "${engine}" == "unreal3" ]||[ "${engine}" == "unreal2" ]||[ "${engine}" == "unreal" ]||[ "${shortname}" == "fctr" ]||[ "${shortname}" == "mumble" ]||[ "${shortname}" == "wurm" ]||[ "${shortname}" == "jc2" ]||[ "${shortname}" == "jc3" ]; then fn_stop_graceful_ctrlc - elif [ "${engine}" == "source" ]||[ "${engine}" == "quake" ]||[ "${engine}" == "idtech2" ]||[ "${engine}" == "idtech3" ]||[ "${engine}" == "idtech3_ql" ]||[ "${shortname}" == "jc2" ]||[ "${shortname}" == "pz" ]||[ "${shortname}" == "rw" ]; then + elif [ "${engine}" == "source" ]||[ "${engine}" == "quake" ]||[ "${engine}" == "idtech2" ]||[ "${engine}" == "idtech3" ]||[ "${engine}" == "idtech3_ql" ]||[ "${shortname}" == "pz" ]||[ "${shortname}" == "rw" ]; then fn_stop_graceful_cmd "quit" 30 fi } fn_stop_ark(){ - maxpiditer=15 # The maximum number of times to check if the ark pid has closed gracefully. + # The maximum number of times to check if the ark pid has closed gracefully. + maxpiditer=15 info_config.sh if [ -z "${queryport}" ]; then fn_print_warn "No queryport found using info_config.sh" @@ -226,8 +227,7 @@ fn_stop_ark(){ if [ "${#queryport}" -gt 0 ] ; then for (( pidcheck=0 ; pidcheck < ${maxpiditer} ; pidcheck++ )) ; do pid=$(netstat -nap 2>/dev/null | grep "^udp[[:space:]]" | grep ":${queryport}[[:space:]]" | rev | awk '{print $1}' | rev | cut -d\/ -f1) - # - # check for a valid pid + # Check for a valid pid. pid=${pid//[!0-9]/} let pid+=0 # turns an empty string into a valid number, '0', # and a valid numeric pid remains unchanged. @@ -235,8 +235,8 @@ fn_stop_ark(){ fn_print_dots "Process still bound. Awaiting graceful exit: ${pidcheck}" sleep 0.5 else - break # Our job is done here - fi # end if for pid range check + break + fi done if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then # The process doesn't want to close after 20 seconds. @@ -244,8 +244,8 @@ fn_stop_ark(){ fn_print_error "Terminating reluctant Ark process: ${pid}" kill -9 ${pid} fi - fi # end if for port check -} # end of fn_stop_ark + fi +} fn_stop_teamspeak3(){ fn_print_dots "${servername}" @@ -253,7 +253,6 @@ fn_stop_teamspeak3(){ "${serverfiles}"/ts3server_startscript.sh stop > /dev/null 2>&1 check_status.sh if [ "${status}" == "0" ]; then - # Remove lockfile rm -f "${rootdir}/${lockselfname}" fn_print_ok_nl "${servername}" fn_script_log_pass "Stopped ${servername}" @@ -267,12 +266,11 @@ fn_stop_tmux(){ fn_print_dots "${servername}" fn_script_log_info "tmux kill-session: ${servername}" sleep 0.5 - # Kill tmux session tmux kill-session -t "${servicename}" > /dev/null 2>&1 sleep 0.5 check_status.sh if [ "${status}" == "0" ]; then - # ARK doesn't clean up immediately after tmux is killed. + # ARK does not clean up immediately after tmux is killed. # Make certain the ports are cleared before continuing. if [ "${shortname}" == "ark" ]; then fn_stop_ark @@ -285,10 +283,9 @@ fn_stop_tmux(){ fi } -# checks if the server is already stopped before trying to stop. +# Checks if the server is already stopped. fn_stop_pre_check(){ -# Is the server already stopped - if [ "${status}" == "0" ]; then # $status comes from check_status.sh, which is run by check.sh for this command + if [ "${status}" == "0" ]; then fn_print_info_nl "${servername} is already stopped" fn_script_log_error "${servername} is already stopped" elif [ "${shortname}" == "ts3" ]; then @@ -296,7 +293,7 @@ fn_stop_pre_check(){ else fn_stop_graceful_select fi - # Check status again, a stop tmux session if needed + # Check status again, a stop tmux session if needed. check_status.sh if [ "${status}" != "0" ]; then fn_stop_tmux @@ -308,7 +305,7 @@ sleep 0.5 check.sh info_config.sh fn_stop_pre_check -# Remove lockfile +# Remove lockfile. if [ -f "${rootdir}/${lockselfname}" ]; then rm -f "${rootdir}/${lockselfname}" fi