Skip to content

Instantly share code, notes, and snippets.

@wuftymerguftyguff
Created February 13, 2018 23:15
Show Gist options
  • Save wuftymerguftyguff/d8f24ecd592fb836f2492b01f7eec6e4 to your computer and use it in GitHub Desktop.
Save wuftymerguftyguff/d8f24ecd592fb836f2492b01f7eec6e4 to your computer and use it in GitHub Desktop.
bash script skeleton to help with the basics (whatever that means)
#!/usr/bin/env bash
############################
############################
############################
# Script Skeleton
# does nothing
# copy it and change it
# its supposed to be a starting
# point for your own scripts
############################
############################
############################
############################
MYPIDFILE="/tmp/${0##*/}.pid"
LOGFILE="${0##*/}.log"
ALARMALIAS="basis"
main()
{
is_interactive
############################
logit "Put Your Code Here"
sleep 10
############################
}
############################
# put your functions here
############################
############################
############################
############################
############################
####### STANDARD FUNCTIONS #
############################
############################
############################
############################
logit() # Provide Standard Logging of Messages
{
echo "`date '+%b %e %X'` - Node \"`hostname`\": $*"
}
mail_alarm()
{
echo "`date '+%b %e %X'` - Node \"`hostname`\": $*" | mailx -s"${0##*/} ALARM" $ALARMALIAS
}
is_interactive()
{
if /usr/bin/env tty > /dev/null
then
logit "Not running in background putting messages to standard output."
else
LOGDIR=$(dirname ${LOGFILE})
if [ ! -d ${LOGDIR} ]
then
mkdir -p ${LOGDIR}
fi
exec 1>>$LOGFILE
exec 2>>$LOGFILE
fi
}
is_up()
{
HOST=$1
logit Checking to see if $1 is UP
if /usr/sbin/ping $HOST -n 5 | /usr/bin/grep -q "100% packet loss"
then
logit $1 is DOWN
return 1
else
logit $1 is UP
return 0
fi
}
trap 'rm -f "$MYPIDFILE;exit 2" >/dev/null 2>&1' 1 2 3 13 15
MYPID=$$
echo $MYPID >> $MYPIDFILE
read INPUTPID < $MYPIDFILE
if [[ "$INPUTPID" != "$MYPID" ]] && ps -fp $INPUTPID 1>/dev/null 2>&1 && ps -fp $INPUTPID | grep -q ${0##*/}
then
echo "Already Running on PID $INPUTPID"
mail_alarm It seems that this script is already running on PID $INPUTPID, I have quit to avoid a conflict, maybe I should be run less often, or the
re is another problem.
else
main
rm -f $MYPIDFILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment