#!/bin/sh
# file: sh_stub.sh
umask 000 ;
# echo all the commands
# debug
set -x
# exit on single error
# debug
set -e
#
# read the IniFile, which is host dependant and set the variables
# report the set variables
doSetVars(){
MyDir=`dirname $0`
cd $MyDir
MyDir=`pwd`
Tmp="$MyDir/tmp/tmp.$$"
mkdir -p $Tmp #create the tmp dir if it does not exist
set >$Tmp/variables.before
MyName=`basename $0`
# boom boom boom where does the ProductBaseDir start ?!
for i in {1..5} ; do cd .. ;done ;
ProductBaseDir=`pwd`
cd $MyDir
IniFile="$MyDir/$MyName.`hostname`.conf"
IniSection=MainSection
LogFile="$MyDir/$MyName.log"
# get the machine / host specific configuration
doParseIniFile
#source $0
set > $Tmp/variables.after
doLog " Using the following vars :"
cmd="comm -13 $Tmp/variables.before $Tmp/variables.after"
doRunCmdAndLog $cmd
rm -f $Tmp/variables.before $Tmp/variables.after
rm -f $Tmp/variables.before $Tmp/variables.before
}
#eof function doSetVars
# parse the host dependant ini file
doParseIniFile(){
eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
-e 's/;.*$//' \
-e 's/[[:space:]]*$//' \
-e 's/^[[:space:]]*//' \
-e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
< $IniFile \
| sed -n -e "/^\[$IniSection\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`
}
#eof function doParseIniFile
# call by doRunCmdAndLog $cmd
doRunCmdAndLog(){
cmd="$*" ;
doLog "running cmd : \"$cmd\""
Msg=$($cmd 2>&1)
[ $? -eq 0 ] || doLog "ERROR : Failed to run the following command \"$cmd\" with the following output \"$Msg\" !!!"
doLog " DEBUG : cmdoutput : \"$Msg\""
}
#eof function doRunCmdAndLog
#call by doRunCmdOrExit $cmd
doRunCmdOrExit(){
cmd="$*" ;
doLog "running cmd : \"$cmd\""
Msg=$($cmd 2>&1)
# if error occured during the execution exit with error
[ $? -eq 0 ] || doExit "ERROR : FATAL : Failed to run the following command \"$cmd\" with the following output \"$Msg\" !!!"
#if no error occured just log the message
doLog " DEBUG : cmdoutput : \"$Msg\""
}
#eof function doRunCmdOrExit
# echo passed params and print them to a log file and to the screen
# with a nice date the the current process id
doLog(){
# check terminal if exists echo
test -t 1 && echo "`date +%Y.%m.%d-%H:%M:%S` [$$] @`hostname` $*"
# check LogFile and
test -z $LogFile || {
echo "`date +%Y.%m.%d-%H:%M:%S` [$$] @`hostname` $*" >> $LogFile
} #eof test
}
# eof function doLog
#
# exit with passed status and message
doExit(){
ExitStatus=0
case $1 in
[0-9]) ExitStatus="$1"; shift 1;;
esac
Msg="$*"
test "$ExitStatus" = "0" || Msg="ERROR: $Msg"
doLog "$Msg"
exit $ExitStatus
}
#eof function Exit
#
# the actions to be performed befor the run
doCleanBeforeRun(){
doLog "DEBUG START function ====== doCleanBeforeRun"
#get the PID of the previous run pid
test -f "$MyDir/PID" && PreviousRunPID=`cat $MyDir/PID`
test -f "$MyDir/PID" && kill -s 9 "$PreviousRunPID"
# set the PID of the current process
echo "$$" > "$MyDir/PID"
doLog "DEBUG STOP function ====== doCleanBeforeRun"
}
#eof function doCleanBeforeRun
#
# Provide here a description of this function what is does etc
doFunctionName(){
doLog "DEBUG START function ====== doFunctionName"
# add here custom logic
doLog "DEBUG STOP function ====== doFunctionName"
}
doCleanAfterRun(){
doLog "DEBUG START function ====== doCleanAfterRun"
#if the PID file exists remove it
test -f "$MyDir/PID" && rm -f "$MyDir/PID"
# set the PID of the current process
doLog "DEBUG STOP function ====== doCleanAfterRun"
}
#eof function doCleanBeforeRun
#
# kills earlier instances of this script if they are running
# by using the base name of the script
doKillOlderMeIfRunning(){
doLog "DEBUG START function ======= doKillOlderMeIfRunning "
# kill a process by name
ProcNameToKill=$MyName
OLD_PIDs=$(ps -ef | grep $ProcNameToKill | grep -v "grep $ProcNameToKill" | perl -ne 'split /\s+/;print "$_[2] "| sort -n')
doLog "OLD_PIDs is $OLD_PIDs"
for OLD_PID in `echo "$OLD_PIDs"`
do
[ $OLD_PID -ne $$ ] && doLog "OLD_PID : $OLD_PID running -> has to be shutdown "
cmd="kill -s 9 $OLD_PID"
[ $OLD_PID -ne $$ ] && doRunCmdAndLog "$cmd"
done
ps -ef | grep "$ProcNameToKill" | grep -v "grep $ProcNameToKill"
#check the running left processes
cmd="ps -ef | grep \"$ProcNameToKill\" | grep -v \"grep $ProcNameToKill\""
#doRunCmdAndLog $cmd
doLog "DEBUG STOP function ======= doKillOlderMeIfRunning "
}
#eof function doKillOlderMeIfRunning
#
# sends an e-mail with the latest n configured lines from the log
doSendReport(){
doLog "DEBUG START function ======= doSendReport"
for Email in $Emails
do
tail -n $NumberOfLinesFromLogFileToReport $LogFile | mailx -s "tail -n $NumberOfLinesFromLogFileToReport $LogFile" $Email
done
doLog "DEBUG STOP ======= doSendReport"
}
#eof function doSendReport
# the main function called
main(){
doSetVars
doKillOlderMeIfRunning
doCleanBeforeRun
doFunctionName
doCleanAfterRun
doSendReport
doExit 0 " STOP MAIN "
}
#eof function main
# Action !!!
main
# Purpose:
# to provifde a basic stub / template sh to start development from on HP-UX
#
# has the following features:
# - prints the set in the script variables
# - separation of host specific vars into $0.`hostname`.conf file
# - doLog function for both xterm and log file printing
# - killing of older instances
# Prerequisites:
# HP-UX B.11.31 U ia64
# sh , perl , sed , grep
# VersionHistory :
# 1.1.0 --- 2012-06-18 12:00:15 --- Yordan Georgiev --- doMethodName + utility methods
# 1.0.0 --- 2012-06-15 08:40:35 --- Yordan Georgiev --- Initial version
#
# EOF File:sh_stub.sh
syntax highlighted by Code2HTML, v. 0.9.1
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!