views:

354

answers:

4

I have an entry in my crontab that looks like this:

0 3 * * * pg_dump mydb | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz

That script works perfectly when I execute it from the shell, but it doesn't seem to be running every night. I'm assuming there's something wrong with the permissions, maybe crontab is running under a different user or something. How can I debug this? I'm a shared hosting environment (WebFaction).

+2  A: 

Always use full paths in crontab entrties. For example, /usr/bin/gzip. You will also need to do that for pg_dump and date.

James Roth
Also, replace the `~` with the real path to your home directory.
JayM
+1  A: 

When you say it doesn't work, what do you mean? Does it not generate the file at all or is it empty?

If your system is set up correctly, crontab should send you an email if your command generated any output.

Try something like this to verify crontab is running. It will touch the file every minute.

* * * * * touch /tmp/foo

And check your paths like James mentioned.

JayM
Does not generate the file. I'm sure crontab is running.
Mark
Are you sure you are allowed access to crontab? I know some shared hosting providers don't allow it.You should probably talk to your hosting company about this.One thing you can check is to see if the files `/etc/cron.allow` and `/etc/cron.deny` exist. If `cron.allow` does exist and your username is not in it, you can't use crontab. And if `cron.deny` exists, you don't want your username listed in it.
JayM
My other crontabs work. WebFaction is cool about that. They give lots of power to their shared users.
Mark
A: 

If this is in something like /etc/crontab, make sure the user is included:

0 3 * * * <user_goes_here> pg_dump mydb | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz
gms8994
+3  A: 

You need to escape "%" characters in crontab entries with backslashes- see the crontab(5) manpage. I've had exactly the same problem.

For example:

0 7 * * * mysqldump usblog | bzip2 -c > usblog.$(date --utc +\%Y-\%m-\%dT\%H-\%M-\%SZ).sql.bz2

Do you not get emails of cron errors? Not even if you put "[email protected]" in the crontab?

You may also need to set PATH in your crontab if pg_dump or gzip isn't on the system default path (so use "type pg_dump" to check where they are, crontab usually only runs commands in /bin or /usr/bin by default)

araqnid
Appears to work. Thanks !!
Mark