PDA

View Full Version : cron with $(date +"%a") ?


Sgurd
February 28th, 2007, 12:10 PM
The following cron entry doesn't appear to run for me:
0 0 * * mon,tue,wed,thu,fri mysqldump mydb > /root/mysqldumps/$(date +"%a").mysql

If I remove the ' +"%a"' it works find.

Can I have a formatted date for the filename in a cron entry?

Thanks for any help - JD

mannheim
February 28th, 2007, 12:14 PM
What happens if you remove the double-quotes?

Sgurd
February 28th, 2007, 12:25 PM
Thanks for the reply.

I removed the double-quotes and I still do not get a dump.

Wallace
February 28th, 2007, 02:11 PM
I don't know if you can do what you want, but there is another way (which is what I do):

Write a script to do the mysql dump, and then schedule that script via cron.

Something like:

mybackup.sh:

#!/bin/sh

mysqldump mydb > /root/mysqldumps/$(date +%a).mysql

and then run mybackup.sh via cron.

mannheim
February 28th, 2007, 05:32 PM
This just worked for me: escape the "%" character with a backslash, like this:

* * * * echo foo > /home/username/test.$( date + \%a )

I've no idea why.

MJN
February 28th, 2007, 06:18 PM
It's because the % character in crontabs indicate new lines.

The problem, along with a solution (note that \ has its drawbacks), is described in full at http://www.hcidata.info/crontab.htm (it even uses a MySQL entry as an example!)

I personally like the 'use a seperate script' workaround like Wallace suggested as then you have the benefit of none of these 'cron peculiarities' as well as making it trivial to make the cron actions even more elaborate with ease.

Mathew

Sgurd
March 1st, 2007, 10:46 AM
Thanks for the replies.
I'll go with the script route.
- JD