Monday, February 6, 2012

Cron Not Running Commands With $(date +%Y%m%d-%H%M)

I have a script that runs in the shell with no problems. But when I put it in cron, it fails to execute. Here's my crontab entry.

00 00 * * 0 /oracle/flash_recovery_area/scripts/rman-level0.sh PROD fb4srv > /oracle/flash_recovery_area/PROD/logs/PROD-LVL0-$(date +%Y%m%d-%H%M).log


Upon investigating the cron log, (I usually just check the mail) I saw this:

& 1
Message 1:
From oracle@somedomain.com Tue Feb 7 00:00:02 2012
Date: Tue, 7 Feb 2012 00:00:02 +0800
From: root@somedomain.com (Cron Daemon)
To: oracle@somedomain.com
Subject: Cron /oracle/flash_recovery_area/scripts/rman-level1.sh PROD fb4srv > /oracle/flash_recovery_area/PROD/logs/PROD-LVL1-$(date +
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:

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

&

So it seems, cron doesn't recognize the values after the + sign. And so, clearly, the error was because of the following characters: %Y%m%d-%H%M).log

Looking at the characters above, my guess is that the % sign was causing the problem. So, I escaped them or replaced them with \%.

My crontab entry now looked like this:

00 00 * * 0 /oracle/flash_recovery_area/scripts/rman-level0.sh PROD fb4srv > /oracle/flash_recovery_area/PROD/logs/PROD-LVL0-$(date +\%Y\%m\%d-\%H\%M).log

And this solved my problem. The script now runs in cron.

No comments:

Post a Comment