Cron job with $(date) command and redirect won't run

This is the cron job I'm trying to run nightly:

0   20  *   *   *   /data/code/scripts/foo.sh >/root/foo.$(date +%Y-%m-%d-%T).log 2>&1

When I run it from the command line, it works fine. But Cron doesn't appear to even try and run it.

Thoughts?


Solution 1:

The % symbols need to be escaped in cron jobs.

Put a backslash in front of each one.

Late edit:

I'm not sure how I missed this before but you are using a bash syntax for creating a subshell to run your date command in. Since cron is not bash, this won't work. It will work if you change your cron job to this:

0   20  *   *   *   /data/code/scripts/foo.sh >/root/foo.`date +\%Y-\%m-\%d-\%T`.log 2>&1

Alternatively, you could do the output redirection from within the script or (if that's not appropriate) write a wrapper script that calls the above one and does the output redirection.

Solution 2:

You will possibly also need to use the full path to the date command.

It's probably located at /bin/date, you can check where it is on your box with which date.