Skip to content

Instantly share code, notes, and snippets.

@Code-Crash
Last active June 9, 2022 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Code-Crash/9d4548e2a966855b1205731f2ad35a71 to your computer and use it in GitHub Desktop.
Save Code-Crash/9d4548e2a966855b1205731f2ad35a71 to your computer and use it in GitHub Desktop.
Script Development Snippet
## General Files/Folder to ignore
!README.md
*.properties
!example.properties
!example-logger.properties
logs
*.log
*.txt
.lastrun
### VS Code ###
.vscode/
### MacOS ###
.DS_Store

Script Development Snippet

Overview

This gist is a snippet for creating any scripts, it contains the default support for property env files and logger on specific location

Setup Guide

  1. Create a respective env property files, as of now we support local.properties, dev.properties, stag.properties and prod.properties, you can use example.properties for default properties and on top of that, you can add more and update the script accordingly

  2. Add your properties to respective property files and update the script accordingly to get the property

  3. Create logger.properties from example-logger.properties file and update the logger path where you want to dump your logs

  4. Now to run the script you can use any follows commands to run this script, before running just make sure all the script has executable permission (chmod +x script.sh logger.sh etc..)

    # Command
    > ./script.sh --target [ENV_NAME]
    # Examples
    ./script.sh # Default would be local env means local.properties will be used, make sure you have it
    ./script.sh --target dev # Targeting develop environment 
    ./script.sh --target stag # Targeting staging environment 
    ./script.sh --target prod # Targeting production environment
    
  5. Now you can add your code in _start_ function in script.sh file.

  6. That's it, Now, validate the logs folder and check if everything looks fine.

Cron Setup [Optional]

  1. If you want to setup a crontab for this script, please use below command after crontab -e

    # Command (For Every 5 Minutes CRONJOB)
      */5 * * * * /[FULL_PATH_TO_SCRIPT]/script.sh --target [ENV_NAME]
    # Examples 1 (For Every 5 Minutes CRONJOB)
      */5 * * * * /opt/script/script.sh --target dev
    # Example 2 (This will run job at 12:00 am every day)
      0 0 * * * /opt/script/script.sh --target stag

TODO:

  1. Improve Logger
  2. UPDATE README.md
#==========================================================
# logger properties
#==========================================================
logger.path=logs
logger.prefix=logger
# LOGGER LEVELS : DEBUG, INFO, ERROR
logger.level=DEBUG
#==========================================================
# Script properties (TODO: It will get converted into env based properties like dev.properties, stag.properties, prod.properties, local.properties)
#==========================================================
script.version=0.0.1-beta
script.env=local
script.name=zScriptName
script.timezone=Asia/Kolkata
#!/usr/bin/env bash
# Please Use Google Shell Style: https://google.github.io/styleguide/shell.xml
# ---- Start unofficial bash strict mode boilerplate
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o errexit # always exit on error
set -o errtrace # trap errors in functions as well
set -o pipefail # don't ignore exit codes when piping output
set -o posix # more strict failures in subshells
# set -x # enable debugging
IFS=$'\n\t'
# ---- End unofficial bash strict mode boilerplate
# ASCII color constants:
NO_COLOR='\033[0m' # No Color
BLACK_COLOR='\033[0;30m'
CYAN_COLOR='\033[0;36m'
LIGHT_CYAN_COLOR='\033[1;36m'
GREEN_COLOR='\033[0;32m'
LIGHT_GREEN_COLOR="\033[1;32m"
RED_COLOR='\033[0;31m'
LIGHT_RED_COLOR='\033[1;31m'
BLUE_COLOR='\033[0;34m'
LIGHT_BLUE_COLOR='\033[1;34m'
YELLOW_COLOR='\033[0;33m'
LIGHT_YELLOW_COLOR='\033[1;33m'
PURPLE_COLOR='\033[0;35m'
LIGHT_PURPLE_COLOR='\033[0;35m'
# Method to return script self path (absolute path)
getAbsolutePath() {
# echo $([[ $1 = /* ]] && echo "$1" || echo "$PWD/")
echo "$(cd $(dirname "$1");pwd)"
}
SCRIPT_ABSOLUTE_PATH=$(getAbsolutePath "$0")
PROPERTY_FILE="$SCRIPT_ABSOLUTE_PATH/logger.properties" # env file properties
# Return property to use a a variable like env variables
getProperty() {
PROP_KEY=$1
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
echo $PROP_VALUE
}
# ------------------------------------------------------ LOGGER OPERATIONS ------------------------------------------------------
LOGGER_LEVEL=$(getProperty "logger.level")
LOGGER_PATH=$(getProperty "logger.path")
LOGGER_PREFIX=$(getProperty "logger.prefix")
LOGGER_TIMESTAMP=$(date +%s)
_LOGGER_INITIAL_="SCRIPT"
loggerSetup() {
[ -d $LOGGER_PATH ] || mkdir -p $LOGGER_PATH
}
# If logger level is not set for console, we still have to show `last time script executed` and `script complted successfully`
ENABLED_ECHO=0 # Default disabled
if [[ ${LOGGER_LEVEL} != 'DEBUG' ]]; then
ENABLED_ECHO=1
fi
function _info_ {
now=$(date +%s)
_LOGGER_=$2
if [[ $_LOGGER_ == "" ]]; then
_LOGGER_=$_LOGGER_INITIAL_
fi
echo "[${_LOGGER_}] [info]" `date -d @$now +%Y-%m-%dT%H:%M:%S` $1 >> "${LOGGER_PATH}/${LOGGER_PREFIX}_${LOGGER_TIMESTAMP}.log"
if [[ ${LOGGER_LEVEL} == 'DEBUG' ]] || [[ ${LOGGER_LEVEL} == 'INFO' ]]; then
echo "[${_LOGGER_}] ${LIGHT_GREEN_COLOR}[info]"${LIGHT_BLUE_COLOR} `date -d @$now +%Y-%m-%dT%H:%M:%S` ${LIGHT_CYAN_COLOR}$1${NO_COLOR}
fi
}
function _error_ {
now=$(date +%s)
_LOGGER_=$2
if [[ $_LOGGER_ == "" ]]; then
_LOGGER_=$_LOGGER_INITIAL_
fi
echo "[${_LOGGER_}] [error]" `date -d @$now +%Y-%m-%dT%H:%M:%S` $1 >> "${LOGGER_PATH}/${LOGGER_PREFIX}_${LOGGER_TIMESTAMP}.log"
if [[ ${LOGGER_LEVEL} == 'DEBUG' ]] || [[ ${LOGGER_LEVEL} == 'ERROR' ]]; then
echo "[${_LOGGER_}] ${LIGHT_RED_COLOR}[error]"${LIGHT_BLUE_COLOR} `date -d @$now +%Y-%m-%dT%H:%M:%S` ${LIGHT_CYAN_COLOR}$1${NO_COLOR}
fi
}
# Setup logger
loggerSetup
#!/usr/bin/env bash
source $(dirname "$0")/logger.sh
_info_ "########################################## SCRIPT START ##########################################"
ENV="local" # Default environment
# Read the command line arguments to know the ENV
while [[ "$#" -gt 0 ]]; do
if [[ $1 == "--target" ]] && [[ $2 == "local" ]] || [[ $2 == "dev" ]] || [[ $2 == "stag" ]] || [[ $2 == "prod" ]]; then
ENV=$2
fi
break # Break the loop after first iteration only
done
# ---------------------------------- Global Variables Declarations ----------------------------------
SCRIPT_ABSOLUTE_PATH=$(getAbsolutePath "$0")
PROPERTY_FILE="$SCRIPT_ABSOLUTE_PATH/$ENV.properties" # env file properties
# Variables
VERSION=$(getProperty "script.version")
ENV=$(getProperty "script.env")
SCRIPT_NAME=$(getProperty "script.name")
TIMEZONE=$(getProperty "script.timezone")
export TZ=$TIMEZONE
UTC_DATE_TIME=$(date -u)
LOCAL_DATE_TIME=$(date -d "$UTC_DATE_TIME")
_info_ "$SCRIPT_NAME ($VERSION) - $ENV env [TZ: $TIMEZONE]"
_info_ "Script Starting UTC: $UTC_DATE_TIME, LOCAL: $LOCAL_DATE_TIME"
_info_ "Property file path: $PROPERTY_FILE"
function _start_ {
# CODE HERE
_info_ "Actual Script Starting From Here" "FIRST_STRING_IN_LOGGER_LOG_LINE"
}
# ------------------------------------------------------ MAIN METHOD ------------------------------------------------------
# Main method which will start the execution
__main__ () {
TIMEFORMAT='Execution Time: %R seconds.'
time {
now=$(date +%s) # Capture Script Execution Start DateTime
LAST_EXECUTED=$now
if [[ -f ./.lastrun ]]; then
LAST_EXECUTED=$(cat ./.lastrun) # we can use to see when the script was last time executed or use to process new files after this last timestamp
else
touch ./.lastrun
echo "$now" >./.lastrun
fi
if [[ $ENABLED_ECHO == 1 ]] && [[ $ENABLED_ECHO == 1 ]] ; then
echo "Last Session Executed: $(date -d @$LAST_EXECUTED 2>/dev/null || date -r $LAST_EXECUTED)"
fi
# Show when last time script got executed before starting execution
_info_ "Last Session Executed: $(date -d @$LAST_EXECUTED 2>/dev/null || date -r $LAST_EXECUTED)"
local start=$(date +%s%N) # Capture script execution start timestamp
_start_
local end=$(date +%s%N) # end timestamp
_info_ "Elapsed time: $(($end-$start)) ns | $((($end-$start)/1000000)) ms | $(echo "scale=3; ($end - $start)/1000000000" | bc -l) seconds "
echo "$now" >./.lastrun # store last time script executed
_info_ "Script Execution Complete!"
if [[ $ENABLED_ECHO == 1 ]]; then
echo "Script Execution Complete!"
fi
}
}
# Execution Start Point
__main__
_info_ "${SCRIPT_NAME} Script Execution Complete!"
_info_ "########################################## SCRIPT COMPLETED ##########################################"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment