Browse Source

fix(stop): fix if statements and refactor comments

pull/2217/head
Daniel Gibbs 7 years ago
parent
commit
0fec527800
  1. 65
      lgsm/functions/command_stop.sh

65
lgsm/functions/command_stop.sh

@ -9,13 +9,13 @@ local commandname="STOP"
local commandaction="Stopping" local commandaction="Stopping"
local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" 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_stop_graceful_ctrlc(){
fn_print_dots "Graceful: CTRL+c" fn_print_dots "Graceful: CTRL+c"
fn_script_log_info "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 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 for seconds in {1..30}; do
check_status.sh check_status.sh
if [ "${status}" == "0" ]; then if [ "${status}" == "0" ]; then
@ -38,13 +38,13 @@ fn_stop_graceful_ctrlc(){
# Attempts graceful shutdown by sending a specified command. # Attempts graceful shutdown by sending a specified command.
# Usage: fn_stop_graceful_cmd "console_command" "timeout_in_seconds" # 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_stop_graceful_cmd(){
fn_print_dots "Graceful: sending \"${1}\"" fn_print_dots "Graceful: sending \"${1}\""
fn_script_log_info "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 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 for ((seconds=1; seconds<=${2}; seconds++)); do
check_status.sh check_status.sh
if [ "${status}" == "0" ]; then if [ "${status}" == "0" ]; then
@ -65,16 +65,15 @@ fn_stop_graceful_cmd(){
sleep 0.5 sleep 0.5
} }
# Attempts graceful of goldsource using rcon 'quit' command. # Attempts graceful shutdown of goldsource using rcon 'quit' command.
# Goldsource 'quit' command restarts rather than shutdown # There is only a 3 second delay before a forced a tmux shutdown
# this function will only wait 3 seconds then force a tmux shutdown. # as Goldsource servers 'quit' command does a restart rather than shutdown.
# preventing the server from coming back online.
fn_stop_graceful_goldsource(){ fn_stop_graceful_goldsource(){
fn_print_dots "Graceful: sending \"quit\"" fn_print_dots "Graceful: sending \"quit\""
fn_script_log_info "Graceful: sending \"quit\"" fn_script_log_info "Graceful: sending \"quit\""
# sends quit # sends quit
tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1 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 for seconds in {1..3}; do
sleep 1 sleep 1
fn_print_dots "Graceful: sending \"quit\": ${seconds}" fn_print_dots "Graceful: sending \"quit\": ${seconds}"
@ -85,7 +84,7 @@ fn_stop_graceful_goldsource(){
sleep 0.5 sleep 0.5
} }
# Attempts graceful of 7 Days To Die using telnet.
fn_stop_telnet_sdtd(){ fn_stop_telnet_sdtd(){
if [ -z "${telnetpass}" ]||[ "${telnetpass}" == "NOT SET" ]; then if [ -z "${telnetpass}" ]||[ "${telnetpass}" == "NOT SET" ]; then
sdtd_telnet_shutdown=$( expect -c ' sdtd_telnet_shutdown=$( expect -c '
@ -122,6 +121,7 @@ fn_stop_telnet_sdtd(){
fi fi
} }
# Attempts graceful shutdown of 7 Days To Die using telnet.
fn_stop_graceful_sdtd(){ fn_stop_graceful_sdtd(){
fn_print_dots "Graceful: telnet" fn_print_dots "Graceful: telnet"
fn_script_log_info "Graceful: telnet" fn_script_log_info "Graceful: telnet"
@ -147,8 +147,8 @@ fn_stop_graceful_sdtd(){
fi fi
done done
# If telnet was successful will use telnet again to check the connection has closed # If telnet shutdown was successful will use telnet again to check
# This confirms that the tmux session can now be killed. # the connection has closed, confirming that the tmux session can now be killed.
if [ -n "${completed}" ]; then if [ -n "${completed}" ]; then
for seconds in {1..30}; do for seconds in {1..30}; do
fn_stop_telnet_sdtd fn_stop_telnet_sdtd
@ -162,8 +162,7 @@ fn_stop_graceful_sdtd(){
sleep 1 sleep 1
fn_print_dots "Graceful: telnet: ${seconds}" fn_print_dots "Graceful: telnet: ${seconds}"
done done
# If telnet failed will go straight to tmux shutdown. # If telnet shutdown fails tmux shutdown will be used, this risks loss of world save.
# If cannot shutdown correctly world save may be lost
else else
if [ -n "${refused}" ]; then if [ -n "${refused}" ]; then
fn_print_error "Graceful: telnet: " fn_print_error "Graceful: telnet: "
@ -189,26 +188,28 @@ fn_stop_graceful_sdtd(){
fn_stop_graceful_select(){ fn_stop_graceful_select(){
if [ "${shortname}" == "sdtd" ]; then if [ "${shortname}" == "sdtd" ]; then
fn_stop_graceful_sdtd fn_stop_graceful_sdtd
elif [ "${engine}" == "Spark" ]; then elif [ "${engine}" == "spark" ]; then
fn_stop_graceful_cmd "q" 30 fn_stop_graceful_cmd "q" 30
elif [ "${shortname}" == "terraria" ]; then elif [ "${shortname}" == "terraria" ]; then
fn_stop_graceful_cmd "exit" 30 fn_stop_graceful_cmd "exit" 30
elif [ "${shortname}" == "mc" ]; then elif [ "${shortname}" == "mc" ]; then
fn_stop_graceful_cmd "stop" 30 fn_stop_graceful_cmd "stop" 30
elif [ "${shortname}" == "mta" ]; then 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 fn_stop_graceful_cmd "quit" 120
elif [ "${engine}" == "goldsource" ]; then elif [ "${engine}" == "goldsource" ]; then
fn_stop_graceful_goldsource 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 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 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 fn_stop_graceful_cmd "quit" 30
fi fi
} }
fn_stop_ark(){ 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 info_config.sh
if [ -z "${queryport}" ]; then if [ -z "${queryport}" ]; then
fn_print_warn "No queryport found using info_config.sh" fn_print_warn "No queryport found using info_config.sh"
@ -226,8 +227,7 @@ fn_stop_ark(){
if [ "${#queryport}" -gt 0 ] ; then if [ "${#queryport}" -gt 0 ] ; then
for (( pidcheck=0 ; pidcheck < ${maxpiditer} ; pidcheck++ )) ; do 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) 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]/} pid=${pid//[!0-9]/}
let pid+=0 # turns an empty string into a valid number, '0', let pid+=0 # turns an empty string into a valid number, '0',
# and a valid numeric pid remains unchanged. # and a valid numeric pid remains unchanged.
@ -235,8 +235,8 @@ fn_stop_ark(){
fn_print_dots "Process still bound. Awaiting graceful exit: ${pidcheck}" fn_print_dots "Process still bound. Awaiting graceful exit: ${pidcheck}"
sleep 0.5 sleep 0.5
else else
break # Our job is done here break
fi # end if for pid range check fi
done done
if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then
# The process doesn't want to close after 20 seconds. # 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}" fn_print_error "Terminating reluctant Ark process: ${pid}"
kill -9 ${pid} kill -9 ${pid}
fi fi
fi # end if for port check fi
} # end of fn_stop_ark }
fn_stop_teamspeak3(){ fn_stop_teamspeak3(){
fn_print_dots "${servername}" fn_print_dots "${servername}"
@ -253,7 +253,6 @@ fn_stop_teamspeak3(){
"${serverfiles}"/ts3server_startscript.sh stop > /dev/null 2>&1 "${serverfiles}"/ts3server_startscript.sh stop > /dev/null 2>&1
check_status.sh check_status.sh
if [ "${status}" == "0" ]; then if [ "${status}" == "0" ]; then
# Remove lockfile
rm -f "${rootdir}/${lockselfname}" rm -f "${rootdir}/${lockselfname}"
fn_print_ok_nl "${servername}" fn_print_ok_nl "${servername}"
fn_script_log_pass "Stopped ${servername}" fn_script_log_pass "Stopped ${servername}"
@ -267,12 +266,11 @@ fn_stop_tmux(){
fn_print_dots "${servername}" fn_print_dots "${servername}"
fn_script_log_info "tmux kill-session: ${servername}" fn_script_log_info "tmux kill-session: ${servername}"
sleep 0.5 sleep 0.5
# Kill tmux session
tmux kill-session -t "${servicename}" > /dev/null 2>&1 tmux kill-session -t "${servicename}" > /dev/null 2>&1
sleep 0.5 sleep 0.5
check_status.sh check_status.sh
if [ "${status}" == "0" ]; then 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. # Make certain the ports are cleared before continuing.
if [ "${shortname}" == "ark" ]; then if [ "${shortname}" == "ark" ]; then
fn_stop_ark fn_stop_ark
@ -285,10 +283,9 @@ fn_stop_tmux(){
fi fi
} }
# checks if the server is already stopped before trying to stop. # Checks if the server is already stopped.
fn_stop_pre_check(){ fn_stop_pre_check(){
# Is the server already stopped if [ "${status}" == "0" ]; then
if [ "${status}" == "0" ]; then # $status comes from check_status.sh, which is run by check.sh for this command
fn_print_info_nl "${servername} is already stopped" fn_print_info_nl "${servername} is already stopped"
fn_script_log_error "${servername} is already stopped" fn_script_log_error "${servername} is already stopped"
elif [ "${shortname}" == "ts3" ]; then elif [ "${shortname}" == "ts3" ]; then
@ -296,7 +293,7 @@ fn_stop_pre_check(){
else else
fn_stop_graceful_select fn_stop_graceful_select
fi fi
# Check status again, a stop tmux session if needed # Check status again, a stop tmux session if needed.
check_status.sh check_status.sh
if [ "${status}" != "0" ]; then if [ "${status}" != "0" ]; then
fn_stop_tmux fn_stop_tmux
@ -308,7 +305,7 @@ sleep 0.5
check.sh check.sh
info_config.sh info_config.sh
fn_stop_pre_check fn_stop_pre_check
# Remove lockfile # Remove lockfile.
if [ -f "${rootdir}/${lockselfname}" ]; then if [ -f "${rootdir}/${lockselfname}" ]; then
rm -f "${rootdir}/${lockselfname}" rm -f "${rootdir}/${lockselfname}"
fi fi

Loading…
Cancel
Save