3

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

rsync -e 'ssh -p 31234' -avlz --delete --stats --progress --copy-links [email protected]:/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?

3 Answers 3

6

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.

2
  • 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, 2009 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, 2009 at 21:30
2

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:

PATH=/sbin:/bin:/usr/sbin:/usr/bin

1

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.

2
  • 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 [email protected], I'm connected to the server without entering the password.
    – Dan
    Nov 10, 2009 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. Nov 11, 2009 at 7:29

You must log in to answer this question.

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