I have a script that is in backup.sh file (chmod 775) :

rsync -e 'ssh -p 31234' -avlz --delete --stats --progress --copy-links admin@foo.xen.prgmr.com:/home/admin/rsync /home/myname/rsync/foo/

When I call it manually, everything works well (and I don't have to enter my passphrase). But when I call it from cron, it just doesn't work.

I used the same setup on the computer of my coworker (who has the same access than me) and it works well.

What's wrong?


I have my passpharseless ssh login setup. I use a command like the following: rsync --delete -auve 'ssh -i /home/red/.ssh/cron_jobs_key' red@othermachine:/source/dir1 /dest/dir2

The key here is "-i /home/red/.ssh/cron_jobs_key" where I have setup an ssh login without a password. Otherwise, I would have to enter my ssh key when I login to my local machine. I generated this ssh key specifically for cron jobs.

  • I tried your script and it doesn't work. I also tried a scp and it doesn't work. Does cron has its own user when it executes? – Dan Nov 10 '09 at 21:11
  • 1
    No, cron runs as the user who is calling the command. BTW, I am not sure if I was clear enough. You need to first generate a new ssh key with an empty passphrase and save it to a unique file. For example, I generated my passphraseless ssh key and saved it to file cron_jobs_key and then copied cron_jobs_key.pub to the remote machines. Then I just specify the specific the key file to use (using -i) with my ssh command in my rsync command, as I showed above. So, have you created a passphraseless ssh key yet? If not, here is the procedure: debian-administration.org/articles/152 – user24524 Nov 10 '09 at 21:30

Some hints:

  • Check you local email (if you configured it). Cron will email the output of scheduled commands.
  • Make sure rsync is in your PATH. You can check this by running the 'env' command in a cron job or include the env command in your existing script. Ie, add a line: "env > /tmp/env_output.txt" and examine that file afterwards.
  • You can define a PATH variable in the top of your crontab:



by mentioning that you dont't have to enter the passphase, I would assume that you are using ssh-agent to enter it once per session.

This won't work from cron as cron has no access to your SSH_AUTH_SOCK variable because it has been started at machine startup, not within your current session.

If you want this to work, you'll have to use a passphraseless key, thats probably what your coworker is using.

  • I don't know what you mean by ssh-agent. What I did is I used ssh-keygen and put the files at the right place on my computer and the server so when I call ssh admin@foo.xen.prgmr.com, I'm connected to the server without entering the password. – Dan Nov 10 '09 at 20:14
  • just to be sure, you never entered any passphrase when creating the key? Try putting -v as ssh parameter and post the output. – Aleksandar Ivanisevic Nov 11 '09 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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