Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm unsure what's going on here:

I've got a backup script which runs fine under root. It produces a >300kb database dump in the proper directory.

When running it as a cron job with exactly the same command however, an empty gzip file appears with nothing in it.

The cron log shows no error, just that the command has been run.

This is the script:

#! /bin/bash

YMD=$(date "+%Y-%m-%d")
su -c "pg_dump -U postgres mydatabasename | gzip -6 > "$DIR/database_backup.$YMD.gz" " postgres

# delete backup files older than 60 days
OLD=$(find $DIR -type d -mtime +60)
if [ -n "$OLD" ] ; then
    echo deleting old backup files: $OLD
    echo $OLD | xargs rm -rfv

When changed to:

 pg_dump -U postgres mydatabasename | gzip -6 > "$DIR/database_backup.$YMD.gz"

The same thing happens.

And the cron job:

01 10 * * * root sh /opt/

It produces a database_backup file, just an empty one. Anyone know what's going on here?


Ok, simplified to this but it's still not working via cron

#! /bin/bash

YMD=$(date "+%Y-%m-%d")

pg_dumpall -U postgres > "$DIR/database_backup.$YMD"


01 10 * * * root /opt/
share|improve this question
Try changing the doublequotes like this: su -c 'pg_dump -U postgres mydatabasename | gzip -6 > "$DIR/database_backup.$YMD.gz" 'postgres – Alan Kuras Apr 4 '12 at 5:45
you dont need to issue "sh" command in cronjob, because you are already declaring that the script should be run in bash.I dont know it it wasnt supposed to be #!/bin/bash without a space. – Alan Kuras Apr 4 '12 at 5:45
The last thing it goes into my mind is that you dont need to issue su command in cronjob file, as you are already running this as root – Alan Kuras Apr 4 '12 at 5:50
Ok - done both of those things, still the same I'm afraid. Also removed the su.. still nothing, argh ! – user705142 Apr 4 '12 at 5:51
up vote 6 down vote accepted

You need to specify the full path to pg_dump -- cron runs it's scripts with a very restrictive PATH by default.

share|improve this answer
Excellent, that was it. Thanks ! – user705142 Apr 4 '12 at 5:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.