Thursday, February 3, 2011

BASH : Logging Techniques

create file similar to syslog
ls *.sh
ls /var/log
cat /log/messages
head -n 2 /var/log/messages -> return the first 2 line from the file
The format has the follwoing format : month Date hour machine nameofthedaemon(application) message
data +%b -> return the current month Ian
date +%d -> day of the month
date +%b\ %d\ %T -> Ian 22 14:32:11
hostname -> returns the name of the computer
nano log1.sh
#!/bin/bash # Ilustrate syslog like logging from our scripts
mydate=`date +%b\ %d\ %T`
myhost=`hostname`
myscript=`$0`
mymessage="Sccess"
echo $mydate $myhost $myscript $mymessage
#END
. log1.sh ->the scrip HANGS because of line myscript=`$0`. It should of been myscript=`basename $0` - the name of the script


#!/bin/bash # Ilustrate syslog like logging from our scripts
mydate=`date +%b\ %d\ %T`
myhost=`hostname`
myscript=`basename $0`
mymessage="Sccess"
echo $mydate $myhost $myscript $mymessage
#END
. log1.sh -> the format is Ian 22 14:32:11 machinename bash Success ; the 'bash' word for the name of the script is incorrect


#!/bin/bash # Ilustrate syslog like logging from our scripts
mydate=`date +%b\ %d\ %T`
myhost=`hostname`
mydirname=`dirname $0`
myscript=`basename $0`
mymessage="Sccess"
echo $mydate $myhost $mydirname $myscript $mymessage
#END
./log1.sh -> Result Ian 22 14:32:11 machinename . log1.sh Success


#!/bin/bash # Ilustrate syslog like logging from our scripts
mydate=`date +%b\ %d\ %T`
myhost=`hostname`
mydirname=`dirname $0`
myscript=`basename $0`
mymessage="Sccess"
echo $mydate $myhost $mydirname $myscript $mymessage >> log1.log
#END
./log1.sh -> a log file is created


#!/bin/bash # Ilustrate syslog like logging from our scripts
mydate=`date +%b\ %d\ %T`
myhost=`hostname`
mydirname=`dirname $0`
myscript=`basename $0`
mymessage="Sccess"
echo $mydate $myhost $mydirname $myscript $mymessage >> `date +%F`.$myscript.log
#END
./log1.sh -> creates file '2011-1-22.log1.sh.log'


nano helloworld.sh
#!/bin/bash
MESSAGE="hello world"
MESSAGE2="hello world2"
clear
date +%F\%r
echo $MESSAGE
echo $MESSAGE2
#inclusion of logging module
if [ $# -eq 0 ]; then . log1.sh; fi
#END
./helloworld.sh


#!/bin/bash # Ilustrate syslog like logging from our scripts
mydate=`date +%b\ %d\ %T`
myhost=`hostname`
mydirname=`dirname $0`
myscript=`basename $0`
if [ $1 = 0]
then
mymessage="Success"
else
mymessage="Failure"
fi
echo $mydate $myhost $mydirname $myscript $mymessage >> `date +%F`.$myscript.log
#END


#!/bin/bash
MESSAGE="hello world"
MESSAGE2="hello world2"
clear
date +%F\%r
echo $MESSAGE
echo $MESSAGE2
#inclusion of logging module
if [ $# -eq 0 ]
then
. log1.sh 0
else
. log1.sh 1
fi # . log1.sh 0 - accepts parameters
#END

No comments:

Post a Comment