Modify script 'base_script.sh'.

This commit is contained in:
Christoph 2022-10-16 13:37:43 +02:00
parent ccfb011a23
commit 62bb096b20
2 changed files with 256 additions and 60 deletions

View File

@ -5,9 +5,13 @@ working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/${script_name%%.*}.conf"
LOCK_DIR="/tmp/$(basename $0).$$.LOCK"
LOCK_DIR="/tmp/${script_name%%.*}.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
# Sending alert messages
#
declare -a alert_email_arr=()
backup_date="$(date +%Y-%m-%d-%H%M)"
declare -a files_backuped_arr=()
declare -a dirs_backuped_arr=()
@ -80,62 +84,54 @@ echononl(){
fatal(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mFatal\033[m ] $*"
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e " \033[31m\033[1mScript was interupted\033[m!"
else
echo -e " [ Fatal ] $*"
echo " [ Fatal ]: $*"
echo ""
echo " Script was terminated...."
fi
echo ""
if $terminal ; then
echo -e " \033[1mScript terminated\033[m.."
else
echo -e " Script terminated.."
fi
echo ""
rm -rf $LOCK_DIR
exit 1
clean_up 1
}
error (){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ] $*"
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo " [ Error ] $*"
echo "[ Error ]: $*"
fi
echo ""
}
warn (){
if $LOGGING || $terminal ; then
echo ""
if $terminal ; then
echo -e " [ \033[33m\033[1mWarn\033[m ] $*"
else
echo " [ Warn ] $*"
fi
echo ""
echo ""
if $terminal ; then
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
else
echo "[ Warning ]: $*"
fi
echo ""
}
info (){
if $LOGGING || $terminal ; then
echo ""
if $terminal ; then
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
else
echo " [ Info ] $*"
fi
echo ""
echo ""
if $terminal ; then
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
else
echo "[ Info ] $*"
fi
echo ""
}
ok (){
if $LOGGING || $terminal ; then
if $terminal ; then
echo ""
if $terminal ; then
echo -e " [ \033[32m\033[1mOk\033[m ] $*"
else
echo " [ Ok ] $*"
echo " [ Ok ] *"
fi
echo ""
fi
@ -185,6 +181,35 @@ blank_line() {
fi
}
is_number() {
return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1);
# - also possible
# -
#[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1
#return $([[ ! -z "${1##*[!0-9]*}" ]])
}
reboot_system() {
# content_type='Content-Type: text/plain;\n charset="utf-8"'
# datum="$(date +"%d.%m.%Y")"
# sender_address="root@$(hostname --long)"
# msg="S*"
#
#
# for _email in ${alert_email_arr[@]} ; do
#
# echo -e "To:${_email}\n${content_type}\nSubject:[Fatal Error] - Reboot System\n${msg}" \
# | sendmail -F "Error `hostname -f`" -f $sender_address $_email
# done
sleep 10
/sbin/reboot -f > /dev/null 2>&1
}
# - Backup file or directory
# -
backup() {
@ -263,20 +288,6 @@ rm_unchanged_backup() {
}
# ----------
# - Jobhandling
# ----------
# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM
# -
trap clean_up SIGHUP SIGINT SIGTERM
# - Create lock directory '$LOCK_DIR"
#
mkdir "$LOCK_DIR"
# ----------
# - Some checks ..
# ----------
@ -337,11 +348,19 @@ fi
# - Give your default values here
# -
LOGGING=false
BATCH_MODE=false
PARAMETER_1="WERT_1"
PARAMETER_2="WERT_2"
DEFAULT_CONFLICTING_SCRIPTS=""
DEFAULT_COMPANY="O.OPEN"
DEFAULT_CONTENT_TYPE='Content-Type: text/plain;\n charset="utf-8"'
DEFAULT_SENDER_ADDRESS="${script_name%%.*}@$(hostname -f)"
DEFAULT_ALERT_EMAIL_ADDRESSES="ckubu@oopen.de"
if [[ -f "$conf_file" ]]; then
source "$conf_file"
else
@ -443,6 +462,136 @@ if $terminal && ! $BATCH_MODE ; then
fi
# ----------
# Set Parameter values
# ----------
[[ -n "$CONFLICTING_SCRIPTS" ]] || CONFLICTING_SCRIPTS="${DEFAULT_CONFLICTING_SCRIPTS}"
[[ -n "$(trim $alert_email_addresses)" ]] || alert_email_addresses=("${DEFAULT_ALERT_EMAIL_ADDRESSES[@]}")
if [[ ${#alert_email_addresses} -gt 0 ]] ; then
for _email in $alert_email_addresses ; do
alert_email_arr+=("$_email")
done
fi
[[ -n "$sender_address" ]] || sender_address="${DEFAULT_SENDER_ADDRESS}"
[[ -n "$content_type" ]] || content_type="${DEFAULT_CONTENT_TYPE}"
[[ -n "$company" ]] || company="${DEFAULT_COMPANY}"
# -------------
# - Job is already running?
# -------------
# - If job already runs, stop execution..
# -
if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal
trap "clean_up 1" SIGHUP SIGINT SIGTERM
else
datum="$(date +"%d.%m.%Y %H:%M")"
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
echo ""
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
echo ""
echo -e " Exiting now.."
echo ""
for _email in ${alert_email_arr[@]} ; do
echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
| sendmail -F "Error `hostname -f`" -f $sender_address $_email
done
exit 1
fi
## ----------
## - Jobhandling
## ----------
#
#LOCK_DIR="/tmp/${script_name%%.*}.$$.LOCK"
#
## - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM
## -
#trap clean_up SIGHUP SIGINT SIGTERM
#
## - Create lock directory '$LOCK_DIR"
##
#mkdir "$LOCK_DIR"
# ----------
# - Stop here, if these give scripts are running
# ----------
if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then
# - Try using a random start delay to prevent (or at least have a small chance) that
# - conflicting scripts will both/all abort if they start at the same time.
# -
# - !! Notice !!
# - This only makes sense if a fixed LOCK directory is used, otherwise the process list
# - (and NOT the LOCK-directory) is used to look for scripts running in parallel.
# -
# - Skip delay if running in an terminal (from copnsole)
# -
if ! $terminal ; then
if [[ "$LOCK_DIR" = "/tmp/${script_name%%.*}.LOCK" ]]; then
_shift="$(( $RANDOM % 10 + 1 ))"
sleep $(( $RANDOM % 25 + $_shift ))
fi
fi
_stop_running=false
for _val in $CONFLICTING_SCRIPTS ; do
IFS=':' read -a _val_arr <<< "${_val}"
_script_name="$(basename ${_val_arr[0]})"
if [[ -n "${_val_arr[1]}" ]] ; then
if [[ "${_val_arr[1]}" = "CHECK_PROCESS_LIST" ]] ; then
check_string_ps="${_val_arr[0]}"
if ps -e f | grep -E "\s+${check_string_ps}" | grep -v grep | grep -v -E "\s+vim\s+" > /dev/null ; then
_stop_running=true
fi
elif [[ -d "${_val_arr[1]}" ]] ; then
_stop_running=true
fi
elif [[ -d "/tmp/${_script_name%%.*}.LOCK" ]]; then
_stop_running=true
fi
if $_stop_running ; then
echo ""
echo "[ Error ]: The \"${_script_name}\" script is currently running, but it conflicts with this script."
echo ""
echo " Exiting now.."
echo ""
clean_up 1
fi # if $_stop_running ; then
done # for _val in $CONFLICTING_SCRIPTS ; do
fi # if [[ ${#CONFLICTING_SCRIPTS} -gt 0 ]] ; then
# ----------
# - Some pre-script tasks ..
# ----------

View File

@ -2,16 +2,63 @@
# - Configuration file for script base_script.sh
# ==========
# - LOGGING
# -
# - Enables/Disables script output. Setting this value to 'true' is
# - only useful if NOT running in a terminal (i.e. as cronjob).
# -
# - If script is running in a terminal, script output is enabled and
# - cannot be disabled.
# -
# - Running this script in a
# -
# - Default value: false
# -
#LOGGING=false
# CONFLICTING_SCRIPTS
#
# The scripts listed here conflict with this script. If one of these scripts
# is currently running, this script will be stopped.
#
# In addition to the script, a LOCK directory can also be specified which is
# connected to it.
#
# If no fixed LOCK directory is connected to the script, set
# this value to the constant 'CHECK_PROCESS_LIST'.
#
# If no value for the LOCK directory is given, the LOCK directory
# '/tmp/<base-script_name>.LOCK' is assumed.
#
#
# Example:
# CONFLICTING_SCRIPTS="
# /root/bin/monitoring/check_webservice_load.sh:CHECK_PROCESS_LIST
# /root/bin/monitoring/check_remote_websites.sh
# "
#
# No defaults
#
#CONFLICTING_SCRIPTS=""
# ---
# - E-Mail settings for sending script messages
# ---
# company
#
# Example: company="Warenform"
#
# Defaults to: company="O.OPEN"
#
#company=""
# sender_address
#
# Defaults to: sender_address="${script_name%%.*}@$(hostname -f)"
#
#sender_address="check_mm_service@$(hostname -f)"
# content_type
#
# Defaults to: content_type='Content-Type: text/plain;\n charset="utf-8"'
#
#content_type='Content-Type: text/plain;\n charset="utf-8"'
# alert_email_addresses
#
# blank separated list of e-mail addresses
#
# Example: alert_email_addresses="ckubu@oopen.de axel@warenform.net"
#
# Defaults to alert_email_addresses="ckubu@oopen.de"
#
#alert_email_addresses="ckubu@oopen.de"