Managing Log files for Cron Jobs
I managed a cron that runs multiple times a day. The author of the script was appending the output logs from each execution to the same file for the day. Now I had the job of analyzing the logs and checking if the process failed, and if so send out email notifications. With the existing log file – I either had to analyze the logging patterns and write some fancy regular expressions to get to the latest log and then analyze the data. Alternatively – I decided to write (not append) to a log file after each execution and use this to analyze data. The execution script would then read from this file and append it to the log file for the day. And at the end of the day – this log file for the day was moved to an archived location.
### Store job execution results in a log file /home/varun.verma/work/linux/someJob.sh > sync.log todayLogFile="/home/varun.verma/work/linux/sync_"`date +%Y-%m-%d`".log" echo $todayLogFile ### Get the current log contents and append to the log file for current day currentLog=`cat /home/varun.verma/work/linux/sync.log` echo "$currentLog" echo "$currentLog" >> $todayLogFile