Super User is a question and answer site for computer enthusiasts and power users. 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've got this rsync command that works just fine when I run it from the command line (Ubuntu Server 12.04.1 LTS). I'd like it to run every hour, on the hour, and output the results (along with errors) to a log file. Here's the command I've put into crontab:

0 * * * * root rsync -av --delete -e "ssh -i /root/.ssh/id_rsa" 
user@host:/path/to/dir/ user@host:/path/to/another/dir/ /destination/dir
--exclude='.DS_Store' &> /var/log/backup/`date '+%Y_%m_%d_%H:%M'`_hourlybackup.log

Despite working exactly as I expected from bash, nothing happens when it's in crontab. It doesn't even write any errors to the file. Running ps aux | grep rsync shows nothing of interest. I can't seem to find the issue. Any ideas?

UPDATE: Thanks to the answers I received I was able to tweak the command so that it works properly, although none of them solved the issue on their own. Here's what I found works this morning:

0 * * * * /usr/bin/rsync -av --delete -e "ssh -i /root/.ssh/id_rsa" 
user@host:/path/to/dir/ user@host:/path/to/another/dir/ /destination/dir
--exclude='.DS_Store' > /var/log/backup/_hourlybackup.log 2>&1   

For some reason the date '+%Y_%m_%d_%H:%M' (enclosed in backticks) is causing the issue. If I remove that from the command, it works just fine ... although it's important to me that I have comprehensive backup logs, so I'd still like to find a solution to this. I'll keep hacking away at it, but if anyone knows, be my guest.

share|improve this question
Which crontab did you put this in? – Paul Jan 30 '13 at 5:35
I put it in root's, using sudo crontab -e. I realized I made a mistake and didn't need to explicitly tell it to use user root, so I removed it, but nothing changed. I also tried copying it into /etc/crontab, but that didn't work either. FYI other, simpler cron commands work fine — echo 'lol' > test.out works, for example. – Jeremy Swinarton Jan 30 '13 at 5:39
That's the way it's supposed to be written. Date is a command and the string is an option. Try entering it in your console. – Jeremy Swinarton Jan 30 '13 at 15:02
I'm using windows now lol – khaled_webdev Jan 30 '13 at 15:30
up vote 1 down vote accepted

The problem, as I mentioned in the update, is with the date command. It turns out, if you're writing the command directly into the crontab, and not into a shell script, you need to escape the % in the date option string. Here's what ended up working:

0 * * * * rsync -av --delete user@host:/path/to/dir 
user@host:/path/to/another/dir /path/to/destination
--exclude='.DS_Store' >
/var/log/backup/`date '+\%Y_\%m_\%d_\%H'`h_hourlybackup.log 2>&1

Thanks for everyone who helped me figure this one out. This command will probably get longer and more complex in the future and will thus be better suited to its own shell script, but for now it's fine directly in crontab, I think.

share|improve this answer

Most of the time it's because you didn't put a full path to your rsync like

/usr/bin/rsync -av ...

Also you can try to create a small shell script and put that command there and just execute that single script from your cron

share|improve this answer
Hm. Tried both, but unfortunately, neither worked. I replaced rsync with /usr/bin/rsync and threw it all inside a shell script. Running the script from the command line works, but it still won't go in crontab. Go figure. – Jeremy Swinarton Jan 30 '13 at 5:14

It could be a path problem, but not likely since root should have /usr/bin in its path.

However, the crontab commands are most probably not run with Bash, but Dash. The &> construct is a "Bashism" [1] [2].


command >& file


command > file 2>&1

to make it work as intended in Dash.

share|improve this answer

put your rsync command into an .sh (shell) script to launch it with crontab

  • search about shell script like this sample


echo $2 echo $1
  • in your script use this syntax to name your backup file

now=$(date +"%m_%d_%Y")

.. &> /var/log/backup/{$now}_hourlybackup.log
share|improve this answer

Would have simply commented to checked answer but new and can't comment yet.

I just wanted to explain why you were seeing this issue. % signifies a newline and any text after the first one is supplied to the command as stdin.

So your cron command:

rsync -av --delete -e "ssh -i /root/.ssh/id_rsa" user@host:/path/to/dir/ \
  user@host:/path/to/another/dir/ /destination/dir --exclude='.DS_Store' \
  &> /var/log/backup/`date '+%Y_%m_%d_%H:%M'`_hourlybackup.log

gets interpreted as:

rsync -av --delete -e "ssh -i /root/.ssh/id_rsa" user@host:/path/to/dir/ \
  user@host:/path/to/another/dir/ /destination/dir --exclude='.DS_Store' \
  &> /var/log/backup/`date '+<<EOI

When the shell tries to execute it, it can't find the second ` as it is now present as part of the command's stdin. The key is to realize all % characters need to be escaped unless you really want newlines.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .