Browse Source

added exit code handling.

pull/914/head
Daniel Gibbs 9 years ago
parent
commit
603fc30fdc
  1. 2
      lgsm/functions/command_dev_debug.sh
  2. 18
      lgsm/functions/command_monitor.sh
  3. 43
      lgsm/functions/command_start.sh
  4. 101
      lgsm/functions/command_stop.sh
  5. 33
      lgsm/functions/core_dl.sh
  6. 23
      lgsm/functions/core_exit.sh
  7. 2
      lgsm/functions/info_parms.sh

2
lgsm/functions/command_dev_debug.sh

@ -11,7 +11,9 @@ function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
if [ -f ".dev-debug" ]; then
rm .dev-debug
fn_print_ok_nl "Disabled dev-debug"
fn_script_log_info "Disabled dev-debug"
else
date > .dev-debug
fn_print_ok_nl "Enabled dev-debug"
fn_script_log_info "Enabled dev-debug"
fi

18
lgsm/functions/command_monitor.sh

@ -14,7 +14,7 @@ fn_monitor_check_lockfile(){
# Monitor does not run it lockfile is not found
if [ ! -f "${rootdir}/${lockselfname}" ]; then
fn_print_info_nl "Disabled: No lock file found"
fn_script_log "Disabled: No lock file found"
fn_script_log_info "Disabled: No lock file found"
echo " * To enable monitor run ./${selfname} start"
exit 1
fi
@ -24,7 +24,7 @@ fn_monitor_check_update(){
# Monitor will not check if update is running.
if [ "$(ps -ef|grep "${selfname} update"|grep -v grep|wc -l)" != "0" ]; then
fn_print_info_nl "SteamCMD is currently checking for updates"
fn_script_log "SteamCMD is currently checking for updates"
fn_script_log_info "SteamCMD is currently checking for updates"
sleep 1
exit
fi
@ -33,7 +33,7 @@ fn_monitor_check_update(){
fn_monitor_msg_checking(){
fn_print_dots "Checking session: "
fn_print_checking_eol
fn_script_log "Checking session: CHECKING"
fn_script_log_info "Checking session: CHECKING"
sleep 1
}
@ -41,17 +41,17 @@ fn_monitor_teamspeak3(){
if [ "${status}" != "0" ]; then
fn_print_ok "Checking session: "
fn_print_ok_eol_nl
fn_script_log "Checking session: OK"
fn_script_log_pass "Checking session: OK"
exit
else
fn_print_fail "Checking session: ${ts3error}: "
fn_print_fail_eol_nl
fn_script_log "Checking session: ${ts3error}: FAIL"
fn_script_log_error "Checking session: ${ts3error}: FAIL"
failurereason="${ts3error}"
alert="restart"
alert.sh
fi
fn_script_log "Monitor is starting ${servername}"
fn_script_log_info "Monitor is starting ${servername}"
sleep 1
fn_restart
}
@ -61,7 +61,7 @@ fn_monitor_tmux(){
if [ "${status}" != "0" ]; then
fn_print_ok "Checking session: "
fn_print_ok_eol_nl
fn_script_log "Checking session: OK"
fn_script_log_pass "Checking session: OK"
# runs gsquery check on game with specific engines.
local allowed_engines_array=( avalanche goldsource realvirtuality source spark unity3d unreal unreal2 )
for allowed_engine in "${allowed_engines_array[@]}"
@ -74,10 +74,10 @@ fn_monitor_tmux(){
else
fn_print_fail "Checking session: "
fn_print_fail_eol_nl
fn_script_log "Checking session: FAIL"
fn_script_log_error "Checking session: FAIL"
alert="restart"
alert.sh
fn_script_log "Monitor is starting ${servername}"
fn_script_log_info "Monitor is starting ${servername}"
sleep 1
command_start.sh
fi

43
lgsm/functions/command_start.sh

@ -12,15 +12,15 @@ function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
fn_start_teamspeak3(){
if [ ! -e "${servercfgfullpath}" ]; then
fn_print_warn_nl "${servercfgfullpath} is missing"
fn_script_log "${servercfgfullpath} is missing"
fn_script_log_warn "${servercfgfullpath} is missing"
echo " * Creating blank ${servercfg}"
fn_script_log "Creating blank ${servercfg}"
fn_script_log_info "Creating blank ${servercfg}"
sleep 2
echo " * ${servercfg} can remain blank by default."
fn_script_log "${servercfgfullpath} can remain blank by default."
fn_script_log_info "${servercfgfullpath} can remain blank by default."
sleep 2
echo " * ${servercfg} is located in ${servercfgfullpath}."
fn_script_log "${servercfg} is located in ${servercfgfullpath}."
fn_script_log_info "${servercfg} is located in ${servercfgfullpath}."
sleep 5
touch "${servercfgfullpath}"
fi
@ -31,8 +31,8 @@ fn_start_teamspeak3(){
check_status.sh
if [ "${status}" != "0" ]; then
fn_print_info_nl "${servername} is already running"
fn_script_log "${servername} is already running"
exit
fn_script_log_error "${servername} is already running"
core_exit.sh
fi
mv "${scriptlog}" "${scriptlogdate}"
@ -48,12 +48,12 @@ fn_start_teamspeak3(){
check_status.sh
if [ "${status}" == "0" ]; then
fn_print_fail_nl "Unable to start ${servername}"
fn_script_log "Unable to start ${servername}"
fn_script_log_fatal "Unable to start ${servername}"
echo -e " Check log files: ${rootdir}/log"
exit 1
core_exit.sh
else
fn_print_ok_nl "${servername}"
fn_script_log "Started ${servername}"
fn_script_log_pass "Started ${servername}"
fi
}
@ -66,7 +66,7 @@ fn_start_tmux(){
# Log rotation
check_status.sh
if [ "${status}" == "0" ]; then
fn_script_log "Rotating log files"
fn_script_log_info "Rotating log files"
if [ "${engine}" == "unreal2" ]; then
if [ -f "${gamelog}" ]; then
mv "${gamelog}" "${gamelogdate}"
@ -80,8 +80,8 @@ fn_start_tmux(){
check_status.sh
if [ "${status}" != "0" ]; then
fn_print_info_nl "${servername} is already running"
fn_script_log "${servername} is already running"
exit
fn_script_log_error "${servername} is already running"
core_exit.sh
fi
# Create lock file
@ -118,11 +118,11 @@ fn_start_tmux(){
check_status.sh
if [ "${status}" == "0" ]; then
fn_print_fail_nl "Unable to start ${servername}"
fn_script_log "Unable to start ${servername}"
fn_script_log_fatal "Unable to start ${servername}"
sleep 1
if [ -s "${scriptlogdir}/.${servicename}-tmux-error.tmp" ]; then
fn_print_fail_nl "Unable to start ${servername}: Tmux error:"
fn_script_log "Tmux error"
fn_script_log_fatal "Unable to start ${servername}: Tmux error:"
echo ""
echo "Command"
echo "================================="
@ -139,30 +139,31 @@ fn_start_tmux(){
echo "================================="
if [ ! $(grep "tty:" /etc/group|grep "$(whoami)") ]; then
echo "$(whoami) is not part of the tty group."
fn_script_log "$(whoami) is not part of the tty group."
fn_script_log_info "$(whoami) is not part of the tty group."
group=$(grep tty /etc/group)
echo ""
echo " ${group}"
fn_script_log "${group}"
fn_script_log_info "${group}"
echo ""
echo "Run the following command with root privileges."
echo ""
echo " usermod -G tty $(whoami)"
echo ""
echo "https://gameservermanagers.com/tmux-op-perm"
fn_script_log "https://gameservermanagers.com/tmux-op-perm"
fn_script_log_info "https://gameservermanagers.com/tmux-op-perm"
else
echo "No known fix currently. Please log an issue."
fn_script_log "No known fix currently. Please log an issue."
fn_script_log_info "No known fix currently. Please log an issue."
echo "https://gameservermanagers.com/issues"
fn_script_log "https://gameservermanagers.com/issues"
fn_script_log_info "https://gameservermanagers.com/issues"
fi
fi
fi
exit 1
core_exit.sh
else
fn_print_ok "${servername}"
fn_script_log "Started ${servername}"
fn_script_log_pass "Started ${servername}"
fi
rm "${scriptlogdir}/.${servicename}-tmux-error.tmp"
echo -en "\n"

101
lgsm/functions/command_stop.sh

@ -21,7 +21,7 @@ fn_stop_graceful_source(){
if [ "${status}" == "0" ]; then
fn_print_ok "Graceful: rcon quit: ${seconds}: "
fn_print_ok_eol_nl
fn_script_log "Graceful: rcon quit: OK: ${seconds} seconds"
fn_script_log_pass "Graceful: rcon quit: OK: ${seconds} seconds"
break
fi
sleep 1
@ -31,7 +31,7 @@ fn_stop_graceful_source(){
if [ "${status}" != "0" ]; then
fn_print_fail "Graceful: rcon quit: "
fn_print_fail_eol_nl
fn_script_log "Graceful: rcon quit: FAIL"
fn_script_log_fail "Graceful: rcon quit: FAIL"
fi
sleep 1
fn_stop_tmux
@ -53,7 +53,7 @@ fn_stop_graceful_goldsource(){
done
fn_print_ok "Graceful: rcon quit: ${seconds}: "
fn_print_ok_eol_nl
fn_script_log "Graceful: rcon quit: OK: ${seconds} seconds"
fn_script_log_pass "Graceful: rcon quit: OK: ${seconds} seconds"
sleep 1
fn_stop_tmux
}
@ -77,7 +77,6 @@ fn_stop_telnet_sdtd(){
expect { eof }
puts "Completed.\n"
')
}
fn_stop_graceful_sdtd(){
@ -98,7 +97,7 @@ fn_stop_graceful_sdtd(){
if [ -n "${refused}" ]; then
fn_print_warn "Graceful: telnet: ${telnetip}: "
fn_print_fail_eol_nl
fn_script_log "Graceful: telnet: ${telnetip}: FAIL"
fn_script_log_warn "Graceful: telnet: ${telnetip}: FAIL"
sleep 1
elif [ -n "${completed}" ]; then
break
@ -114,7 +113,7 @@ fn_stop_graceful_sdtd(){
if [ -n "${refused}" ]; then
fn_print_ok "Graceful: telnet: ${telnetip}: "
fn_print_ok_eol_nl
fn_script_log "Graceful: telnet: ${telnetip}: ${seconds} seconds"
fn_script_log_pass "Graceful: telnet: ${telnetip}: ${seconds} seconds"
break
fi
sleep 1
@ -126,10 +125,10 @@ fn_stop_graceful_sdtd(){
if [ -n "${refused}" ]; then
fn_print_fail "Graceful: telnet: "
fn_print_fail_eol_nl
fn_script_log "Graceful: telnet: ${telnetip}: FAIL"
fn_script_log_fail "Graceful: telnet: ${telnetip}: FAIL"
else
fn_print_fail_nl "Graceful: telnet: Unknown error"
fn_script_log "Graceful: telnet: Unknown error"
fn_script_log_fail "Graceful: telnet: Unknown error"
fi
echo -en "\n" | tee -a "${scriptlog}"
echo -en "Telnet output:" | tee -a "${scriptlog}"
@ -141,7 +140,7 @@ fn_stop_graceful_sdtd(){
fn_script_log "Graceful: telnet: "
fn_print_fail "Graceful: telnet: expect not installed: "
fn_print_fail_eol_nl
fn_script_log "Graceful: telnet: expect not installed: FAIL"
fn_script_log_fail "Graceful: telnet: expect not installed: FAIL"
fi
sleep 1
fn_stop_tmux
@ -160,42 +159,42 @@ fn_stop_graceful_select(){
}
fn_stop_ark(){
MAXPIDITER=15 # The maximum number of times to check if the ark pid has closed gracefully.
info_config.sh
if [ -z $queryport ] ; then
fn_print_warn "no queryport found using info_config.sh"
userconfigfile="${filesdir}"
userconfigfile+="/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini"
queryport=$(grep ^QueryPort= ${userconfigfile} | cut -d= -f2 | sed "s/[^[:digit:].*].*//g")
fi
if [ -z $queryport ] ; then
fn_print_warn "no queryport found in the GameUsersettings.ini file"
return
fi
maxpiditer=15 # The maximum number of times to check if the ark pid has closed gracefully.
info_config.sh
if [ -z "${queryport}" ]; then
fn_print_warn "no queryport found using info_config.sh"
userconfigfile="${filesdir}"
userconfigfile+="/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini"
queryport=$(grep ^QueryPort= ${userconfigfile} | cut -d= -f2 | sed "s/[^[:digit:].*].*//g")
fi
if [ -z "${queryport}" ]; then
fn_print_warn "no queryport found in the GameUsersettings.ini file"
return
fi
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
let pid+=0 # turns an empty string into a valid number, '0',
# and a valid numeric pid remains unchanged.
if [[ $pid -gt 1 && $pid -le $(cat /proc/sys/kernel/pid_max) ]] ; then
fn_print_dots "Process still bound. Awaiting graceful exit: $pidcheck"
sleep 1
else
break # Our job is done here
fi # end if for pid range check
done
if [[ ${pidcheck} -eq ${MAXPIDITER} ]] ; then
# The process doesn't want to close after 20 seconds.
# kill it hard.
fn_print_warn "Terminating reluctant Ark process: $pid"
kill -9 $pid
fi
fi # end if for port check
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
let pid+=0 # turns an empty string into a valid number, '0',
# and a valid numeric pid remains unchanged.
if [[ $pid -gt 1 && $pid -le $(cat /proc/sys/kernel/pid_max) ]] ; then
fn_print_dots "Process still bound. Awaiting graceful exit: $pidcheck"
sleep 1
else
break # Our job is done here
fi # end if for pid range check
done
if [[ ${pidcheck} -eq ${maxpiditer} ]] ; then
# The process doesn't want to close after 20 seconds.
# kill it hard.
fn_print_warn "Terminating reluctant Ark process: $pid"
kill -9 $pid
fi
fi # end if for port check
} # end of fn_stop_ark
fn_stop_teamspeak3(){
@ -208,10 +207,10 @@ fn_stop_teamspeak3(){
# Remove lock file
rm -f "${rootdir}/${lockselfname}"
fn_print_ok_nl "${servername}"
fn_script_log "Stopped ${servername}"
fn_script_log_pass "Stopped ${servername}"
else
fn_print_fail_nl "Unable to stop${servername}"
fn_script_log "Unable to stop${servername}"
fn_script_log_fail "Unable to stop${servername}"
fi
}
@ -227,11 +226,11 @@ fn_stop_tmux(){
# Remove lock file
rm -f "${rootdir}/${lockselfname}"
# ARK doesn't clean up immediately after tmux is killed.
# Make certain the ports are cleared before continuing.
if [ "${gamename}" == "ARK: Survivial Evolved" ]; then
fn_stop_ark
echo -en "\n"
fi
# Make certain the ports are cleared before continuing.
if [ "${gamename}" == "ARK: Survivial Evolved" ]; then
fn_stop_ark
echo -en "\n"
fi
fn_print_ok_nl "${servername}"
fn_script_log "Stopped ${servername}"
else

33
lgsm/functions/core_dl.sh

@ -28,15 +28,15 @@ fn_dl_md5(){
fn_print_fail_eol_nl
echo "${filename} returned MD5 checksum: ${md5sumcmd}"
echo "expected MD5 checksum: ${md5}"
fn_script_log "verifying ${filename} with MD5: FAIL"
fn_script_log "${filename} returned MD5 checksum: ${md5sumcmd}"
fn_script_log "expected MD5 checksum: ${md5}"
fn_script_log_fatal "verifying ${filename} with MD5: FAIL"
fn_script_log_info "${filename} returned MD5 checksum: ${md5sumcmd}"
fn_script_log_info "expected MD5 checksum: ${md5}"
exit 1
else
fn_print_ok_eol_nl
fn_script_log "verifying ${filename} with MD5: OK"
fn_script_log "${filename} returned MD5 checksum: ${md5sumcmd}"
fn_script_log "expected MD5 checksum: ${md5}"
fn_script_log_pass "verifying ${filename} with MD5: OK"
fn_script_log_info "${filename} returned MD5 checksum: ${md5sumcmd}"
fn_script_log_info "expected MD5 checksum: ${md5}"
fi
fi
}
@ -62,26 +62,26 @@ fn_dl_extract(){
local exitcode=$?
if [ ${exitcode} -ne 0 ]; then
fn_print_fail_eol_nl
fn_script_log "extracting download: FAIL"
fn_script_log_fatal "extracting download: FAIL"
echo "${tarcmd}" | tee -a "${scriptlog}"
exit ${exitcode}
core_exit.sh
else
fn_print_ok_eol_nl
fi
}
# Trap to remove file download if canceled before completed
fn_fetch_trap() {
fn_fetch_trap(){
echo ""
echo -ne "downloading ${filename}: "
fn_print_canceled_eol_nl
fn_script_log "downloading ${filename}: CANCELED"
fn_script_log_info "downloading ${filename}: CANCELED"
sleep 1
rm -f "${filedir}/${filename}" | tee -a "${scriptlog}"
echo -ne "downloading ${filename}: "
fn_print_removed_eol_nl
fn_script_log "downloading ${filename}: REMOVED"
exit
fn_script_log_info "downloading ${filename}: REMOVED"
core_exit.sh
}
fn_fetch_file(){
@ -125,15 +125,15 @@ fn_fetch_file(){
if [ ${exitcode} -ne 0 ]; then
fn_print_fail_eol_nl
if [ -f "${scriptlog}" ]; then
fn_script_log "downloading ${filename}: FAIL"
fn_script_log_fatal "downloading ${filename}: FAIL"
fi
echo "${curlcmd}" | tee -a "${scriptlog}"
echo -e "${fileurl}\n" | tee -a "${scriptlog}"
exit ${exitcode}
core_exit.sh
else
fn_print_ok_eol_nl
if [ -f "${scriptlog}" ]; then
fn_script_log "downloading ${filename}: OK"
fn_script_log_pass "downloading ${filename}: OK"
fi
fi
# remove trap
@ -142,6 +142,9 @@ fn_fetch_file(){
fn_print_fail_eol_nl
echo "Curl is not installed!"
echo -e ""
if [ -f "${scriptlog}" ]; then
fn_script_log_fatal "Curl is not installed!"
fi
exit 1
fi
# make file executecmd if executecmd is set

23
lgsm/functions/core_exit.sh

@ -0,0 +1,23 @@
#!/bin/bash
# LGSM core_exit.sh function
# Author: Daniel Gibbs
# Website: https://gameservermanagers.com
lgsm_version="210516"
# Description: handles exiting of LGSM by running and reporting an exit code.
if [ "${exitcode}" != "0" ]; then
if [ "${exitcode}" == "1" ]; then
fn_script_log_fatal "Exiting with exit code: ${exitcode}"
if [ "${exitcode}" == "2" ]; then
fn_script_log_error "Exiting with exit code: ${exitcode}"
if [ "${exitcode}" == "3" ]; then
fn_script_log_warn "Exiting with exit code: ${exitcode}"
else
fn_script_log "Exiting with exit code: ${exitcode}"
fi
else
fn_script_log_pass "Exiting with exit code: ${exitcode}"
fi
exit ${exitcode}

2
lgsm/functions/info_parms.sh

@ -4,7 +4,7 @@
# Website: https://gameservermanagers.com
lgsm_version="210516"
# Description: Gets specific details server parameters.
# Description: Gets specific details from server parameters.
## Examples of filtering to get info from config files
# sed 's/foo//g' - remove foo

Loading…
Cancel
Save