# Function to write to the Log file ################################### write_log() { while read text do LOGTIME=`date "+%Y-%m-%d %H:%M:%S"` # If log file is not defined, just echo the output if [ "$LOG_FILE" == "" ]; then echo $LOGTIME": $text"; else LOG=$LOG_FILE.`date +%Y%m%d` touch $LOG if [ ! -f $LOG ]; then echo "ERROR!! Cannot create log file $LOG. Exiting."; exit 1; fi echo $LOGTIME": $text" | tee -a $LOG; fi done }
It is easy to log any output to such subroutine, just pipe the output as folowing:
echo "Skipping to next copy" | write_log
If you want to also store the same output to another file, you can add another pipe:
echo " ^^^ ERROR while rsync $OUTPUTDIR." | write_log | tee -a $TEMP_DETAIL_FILE
Thanks for saving my day by sharing this function...
ReplyDeletejust a minor correction to initialize the $LOG_FILE variable
Thank you for this script . Also the comment from Harish .
ReplyDelete