Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to time stamp a log file? 1

Status
Not open for further replies.

InigoMontoya

IS-IT--Management
Jun 18, 2003
51
US
I have a script that runs in the rc directory. I would like its output standard error and standard out to be redirected to a logfile but also have it time stamped. I have the redirection down ok but the I can't figure out how to time stamp the entries? Can anyone help me? Thanks.

this is what i have so far:

Code:
/path/to/script/script.ksh >> path/to/log/script.log 2>&1
 
Would adding the following at the very beginning and very end of the script get you what you want:

echo "\nBeginning script.ksh\n" >> script.log
echo `date` >> script.log

...
...

echo `date` >> script.log
echo "\nEnding script.ksh\n" >> script.log

 
You could also create dated logfiles:

crdt=$(date +%Y%m%d%H%M)
echo "\nBeginning script.ksh\n" > script_${crdt}.log
echo `date` >> script_${crdt}.log
...etc...




----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Yes. that would probably be more useful and you wouldn't build up a huge log file.
 
You could create a simple script to add a timestamp to each line of the input pipe, e.g....

#----stamper.ksh
while read LINE
do
STAMP=$(date +'%Y%m%d%H%M%S')
echo $STAMP $LINE
done

Then run your script using...

/path/to/script/script.ksh 2>&1 | stamper.ksh >> script.log

 
If you make your script look like the following, you won't have to redirect anything, it will always ONLY send it's output to a timestamped log file...
Code:
#!/bin/ksh

export TIMESTAMP=$(date '+%Y%m%d%H%M')

export LOGDIR=/path/to/logs
export LOGFILE=${LOGDIR}/script_${TIMESTAMP}.log

# Redirect all script output to the log file
exec 1> ${LOGFILE}
exec 2>&1

# Do script stuff here
# All output, normal or error, goes to the log file.

# Close the log file descriptors
exec 1>&-
exec 2>&-
The only problem is that this will ALWAYS send it to the log file. You could code a command line switch that will selectively do the execs to redirect the output.

You could also just run it like the following...
Code:
/path/script.ksh > /logpath/script_`/bin/date '+%Y%m%d%H%M'`.log 2>&1
That would timestamp the log file in the command that runs it. You could put this in cron and it should work. Make sure you have the full path to [tt]date[/tt].

Hope this helps.

 
What if Ygor and Sambones' code were combined? Is this something that will work?
Code:
exec 1> | stamper.ksh >> ${LOGFILE}
exec 2>&1
 
I don't think you could combine them like that, but you could do something like this...

#!/bin/ksh

function fn_stamper {
while read LINE
do
LOGFILE=/path/to/logs/script_$(date '+%Y%m%d%H%M').log
STAMP=$(date +'%Y%m%d%H%M%S')
echo $STAMP $LINE >> $LOGFILE
done
}

(

# Do script stuff here
# All output, normal or error, goes to the log file(s).

) 2>&1 | fn_stamper

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top