i have a cronjob:
0 9 * * * rsync -a mydir remote_machine:
i installed this with 'crontab -e'. i have an ssh-agent running, and when i execute the rsync command itself it works w/o any user interaction or password entry, but the cronjob fails with the following message:
Date: Wed, 9 Dec 2009 11:11:00 -0600 (CST)
From: Cron Daemon <me@my_machine.my_domain>
To: me@my_machine.my_domain
Subject: Cron <me@my_machine> rsync -a /home/me/mydir remote_machine:
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-with-mic,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at /SourceCache/rsync/rsync-35.2/rsync/io.c(452)
[sender=2.6.9]
why doesn't this work? i know the cronjobs run w/ me as the user (if i run '* * * * * touch /tmp/a' i own the file) so i assume the rsync is logging in as me using my private key...
keychain is what you need! Just install it and add the follow code in your
.bash_profile
(or equivalent):For config.fish (2):
Then use the code below in your script to load the ssh-agent environment variables:
For Fish:
If your key have a passhphrase, keychain will ask you once (valid until you reboot the machine or kill the ssh-agent).
Note: keychain also generates code to
csh
andfish
shells, so just replace the suffix "-sh" to "-csh" or "-fish".Your cron session shell has no knowledge of the ssh agent, so can't talk to it.
When the agent is started, you can put the information needed for the agent someplace for the cron session to pick up.
Example:
Then add your key to the agent.
Now your cron job should do this before attempting to use ssh:
...after which, the ssh session should proceed normally.
I suppose you're using key based auth to authenticate yourself with the remote machine. Try the line below:
Where .ssh/id_rsa is the path to your private key. This is the exact line I'm using to do my backups and it always works fine for me.
Best wishes,
Fabian
I don't have enough rep to vote up the first answer, but it solved the issue that I was having. In terms of ssh-agent, you may already have one running. Here's a script to extract the SSH_AGENT_PID & SSH_AUTH_SOCK from the environment without any additional stuff to save on startup of ssh-agent. (Assumes that you have perl)
Put the following in a script. (for example findagent.pl)
and inside your cron script add the line:
eval `{path to script}/findagent.pl`
As an alternative, instead of using the ssh agent I made my script do export RSYNC_RSH="ssh -i /home/user/.ssh/id_rsa" unset SSH_AGENT_PID unset SSH_AUTH_SOCK before invoking rsync. By putting it in RSYNC_RSH instead of using '-e ...' it made it easy to adjust the id file being used based on the host.
Hope this helps, B