Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I think I'm missing something. I've set up some crontabs to handle rsyncs over ssh in my office to handle some backups, but they don't seem to be running OR logging, for that matter. Am I missing something?

Here are my crontabs:

0 2 * * * idealm /usr/bin/local/rsync -avz --rsync-path=/usr/bin/rsync -e ssh /Ideal\ Machinery\ Database/* i@192.168.0.210:~/db_backup >> /home/idealm/Work/cron_log.txt

^ from the Mac Mini Server to the SME Server

0 2 * * * i /usr/bin/rsync -avz --rsync-path=/usr/local/bin/rsync -e ssh /home/e-smith/files/ibays/drive-i/files/Warehouse\ Pics/Converted\ Warehouse\ Pictures/* idealm@192.168.0.194:/ideal_img >> /home/i/cron_log.txt

^ from the SME Server to the Mac Mini Server

0 2 * * * i /usr/bin/rsync -rvz -e ssh /home/e-smith/files/ibays/drive-i/files/Warehouse\ Pics/* fm-backup@[remote server]:/home/fm-backup/img_all/ >> /home/i/cron_log.txt

0 2 * * * i /usr/bin/rsync -rvz -e ssh ~/db_backup/* fm-backup@[remote server]:~/db_backup/ >> /home/i/cron_log.txt

^ From the SME Server to a remote server running Ubuntu 10.10 (where [remote server] is the censored address)

The thing is, when I run these jobs from bash, everything works perfectly - cron just doesn't seem to be working. Any suggestions?

share|improve this question
    
Try first with a very simple dummy command to write some stuff to standard error, and have cron set up to mail your the standard error output. –  Faheem Mitha Apr 6 '11 at 14:08
    
What is the output of crontab -l –  g33kz0r Apr 6 '11 at 16:36
    
Have you checked your cron logs? Have you tried replacing ~ with the path? –  Cry Havok Apr 6 '11 at 17:05
    
Cron runs those commands at 2:00am in whatever timezone your computer thinks it lives in. Have you waited past that time-of-ay? –  Bruce Ediger Apr 6 '11 at 20:56
add comment

1 Answer 1

up vote 4 down vote accepted

First try some simple cronjobs and build from here:

0 12 * * * user echo 'Hello, World!' >> /tmp/test.log 2>&1
1 12 * * * user ssh anotheruser@anotherhost ls >> /tmp/test.log 2>&1
2 12 * * * user rsync -e ssh anotheruser@anotherhost:/path/to/small/dir /tmp/test-dir >> /tmp/test.log 2>&1

Avoid using too many options at first, but using 2>&1 is important for getting any error messages. You could also use 2>/tmp/test-error.log to send them to a separate file. Normally, cron generates an email if, and only if, there is any output. This mail is sent to the email address mentioned in the MAILTO environment variable set in crontab or to the local unix user the job is run as if MAILTO is not set. If you don't have a mail server installed or are unsure how to access it, then you can just redirect all output to a file instead. Another thing to look at is setting SHELL. By default, cron uses /bin/sh which is normally just fine, and may even be a symlink to /bin/bash on some systems, but if not and you expect to use bash-isms in your command, you can add SHELL=/bin/bash or whatever is appropriate before your job listings.

Another issue I can see with your above crontabs is spaces. Since the commands work at a normal shell, it's probably not the issue, but do be wary of using spaces when using certain remote ssh commands like: ssh server ls "Music Videos", spaces in such commands need to be double escaped due to the fact that both the local shell and remote shell may interpret them. The correct command should be something like this: ssh server ls "Music\\ Videos" or ssh server ls '"Music Videos"'

The last thing I can think of is that ssh is failing authentication. I assume you are using public key authentication. Where is your public key being stored? Is it in ~/.ssh/id_rsa as an unencrypted private key, or do you have it loaded in a SSH Public Key Agent? If it's loaded in an agent because the on-disk copy is encrypted, you will need to make sure SSH_AUTH_SOCK is manually set in crontab to point to an agent that will be running when rsync does. I normally keep my private keys encrypted on-disk and keep ssh-agent running in the background with a fixed socket path. For example, I run ssh-agent -a /tmp/user.agent followed by SSH_AUTH_SOCK=/tmp/user.agent ssh-add .ssh/id_rsa to load in the private key to the agent. I have these two commands in a script that I run manually after each reboot since I need to enter in a passphrase to unlock the private key. Then I have SSH_AUTH_SOCK=/tmp/user.agent in my crontab file for my automatic cronjobs to use.

share|improve this answer
    
+1 for recommending 2>&1. It's always a good idea to send STDERR to a sane location. –  Stefan Lasiewski Apr 6 '11 at 21:06
    
Thanks so much for your thorough explanation - it turns out that I was putting in the wrong username. Everything seems to be working now. –  kivetros Apr 11 '11 at 15:41
add comment

Your Answer

 
discard

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.