diff --git a/testscript-running-30-seconds-with-dynamic-LOCK_DIR.sh b/testscript-running-30-seconds-with-dynamic-LOCK_DIR.sh new file mode 100755 index 0000000..94fd4c6 --- /dev/null +++ b/testscript-running-30-seconds-with-dynamic-LOCK_DIR.sh @@ -0,0 +1,128 @@ +#!/usr/bin/env bash + + +_script="$0" + +LOCK_DIR=`mktemp -d` + + +#--------------------------------------- +#----------------------------- +# Base Function(s) +#----------------------------- +#--------------------------------------- + +fatal(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ] $*" + else + echo -e " [ Fatal ] $*" + 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 +} + +error (){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ] $*" + else + echo " [ Error ] $*" + fi + echo "" +} + +warn (){ + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarn\033[m ] $*" + else + echo " [ Warn ] $*" + fi + echo "" +} + +info (){ + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mInfo\033[m ] $*" + else + echo " [ Info ] $*" + fi + echo "" +} + +ok (){ + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mOk\033[m ] $*" + else + echo " [ Ok ] $*" + fi + echo "" +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + + +#--------------------------------------- +#----------------------------- +# Check some prerequisites +#----------------------------- +#--------------------------------------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true + LOGGING=true +else + terminal=false + LOGGING=false +fi + + +# - Systemd supported ? +# - +systemd=$(which systemd) +systemctl=$(which systemctl) + +systemd_supported=false +if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then + systemd_supported=true +fi + + +#--------------------------------------- +#----------------------------- +# Check if SSH service is running +#----------------------------- +#--------------------------------------- + +if $LOGGING ; then + echo -e "\n Going to run this script for 30 seconds" + echo -e " =======================================" + echo "" + echo -e "\tLOCK_DIR: $LOCK_DIR" + echo "" +fi + +sleep 30 + +rm -rf $LOCK_DIR +exit 0 + + diff --git a/testscript-running-30-seconds-with-static-LOCK_DIR.sh b/testscript-running-30-seconds-with-static-LOCK_DIR.sh new file mode 100755 index 0000000..633f6aa --- /dev/null +++ b/testscript-running-30-seconds-with-static-LOCK_DIR.sh @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + + +script_name="$(basename $(realpath $0))" + +LOCK_DIR="/tmp/${script_name%%.*}.LOCK" + + +#--------------------------------------- +#----------------------------- +# Base Function(s) +#----------------------------- +#--------------------------------------- + +fatal(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ] $*" + else + echo -e " [ Fatal ] $*" + 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 +} + +error (){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ] $*" + else + echo " [ Error ] $*" + fi + echo "" +} + +warn (){ + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarn\033[m ] $*" + else + echo " [ Warn ] $*" + fi + echo "" +} + +info (){ + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mInfo\033[m ] $*" + else + echo " [ Info ] $*" + fi + echo "" +} + +ok (){ + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mOk\033[m ] $*" + else + echo " [ Ok ] $*" + fi + echo "" +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + + +#--------------------------------------- +#----------------------------- +# 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 SIGHUP SIGINT SIGTERM + +else + + msg="A previos instance of script \"`basename $0`\" seems already be running." + + echo "" + if $terminal ; then + echo -e "[ \033[31m\033[1mFatal\033[m ]: $msg" + echo "" + echo -e " \033[31m\033[1mScript was interupted\033[m!" + else + echo " [ Fatal ]: $msg" + echo "" + echo " Script was interupted!" + fi + echo + + exit 1 + +fi + + + +#--------------------------------------- +#----------------------------- +# Check some prerequisites +#----------------------------- +#--------------------------------------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true + LOGGING=true +else + terminal=false + LOGGING=false +fi + + +# - Systemd supported ? +# - +systemd=$(which systemd) +systemctl=$(which systemctl) + +systemd_supported=false +if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then + systemd_supported=true +fi + + +#--------------------------------------- +#----------------------------- +# Check if SSH service is running +#----------------------------- +#--------------------------------------- + +if $LOGGING ; then + echo -e "\n Going to run this script for 30 seconds" + echo -e " =======================================" + echo "" + echo -e "\tLOCK_DIR: $LOCK_DIR" + echo "" +fi + +sleep 30 + +rm -rf $LOCK_DIR +exit 0 + +