DBA Tips Archive for Oracle


Crontab Redirect to Log File With Date in Name

by Jeff Hunter, Sr. Database Administrator


  1. Overview
  2. Solution


The following article should serve as a note on how to include a date string (using the UNIX date command) to a file within a crontab file.

You may have run into this before. You attempt to insert the current date/time to a file within the crontab as follows:

30 12 * * * /u01/app/oracle/bin/rman_backup.pl > /u01/app/oracle/log/rman_backup_`date '+%Y%M%d'`.log 2>&1
You then receive an email from cron with the following error:

/u01/app/oracle/log/rman_backup_`date '

/bin/sh: -c: line 1: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 2: syntax error: unexpected end of file

Ok, so you try to escape the % with \ and try it again. Well, I didn't get an error, but the log file now includes the newly included escape character \:

Even trying to use two escape characters does not do the trick. You will get an error similar to the first by with +\\ at the end of the message.

One solution is to simply use the date command without any formatting, but this is not what most users are looking for when you want a standard convention (standard date format like YYYYMMDD) for log files.

Read on for one possible solution...

The solution that has always worked for me no matter what SHELL I am using or the flavor of UNIX:
30 12 * * * /u01/app/oracle/bin/rman_backup.pl > /u01/app/oracle/log/rman_backup_$(date +\%Y\%m\%d_\%H:\%M:\%S\%z).log 2>&1
If you need only the date (not including the time element):
30 12 * * * /u01/app/oracle/bin/rman_backup.pl > /u01/app/oracle/log/rman_backup_$(date +\%Y\%m\%d).log 2>&1

Copyright (c) 1998-2018 Jeffrey M. Hunter. All rights reserved.

All articles, scripts and material located at the Internet address of http://www.idevelopment.info is the copyright of Jeffrey M. Hunter and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express, prior, written permission. Application to host any of the material elsewhere can be made by contacting me at jhunter@idevelopment.info.

I have made every effort and taken great care in making sure that the material included on my web site is technically accurate, but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.

Last modified on
Friday, 22-Feb-2008 18:18:01 EST
Page Count: 32787