Maintaining log files for Cron Jobs that run multiple times a day

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/ > 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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s