Monday, January 9, 2017

cron - Execute rsync command over ssh with an ssh agent via crontab



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
To: me@my_machine.my_domain
Subject: Cron 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...


Answer



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:



AGENT="ssh-agent -s"
if [ ! -d $HOME/.ssh/agent ]; then
mkdir -p $HOME/.ssh/agent
fi
#

# Start an agent if there isn't one running already.
#
pid=`ps -u$LOGNAME | grep ssh-age | awk '{print $1}'`
if [ -z "$pid" ]; then
$AGENT | grep -v echo > $HOME/.ssh/agent/$HOST & pid=$!
sleep 1 # Let it fork and stuff
fi


Then add your key to the agent.




ssh-add $HOME/.ssh/id_dsa


Now your cron job should do this before attempting to use ssh:



#
# Get our parent to pick up the required SSH env vars.
#
. $HOME/.ssh/agent/$HOST



...after which, the ssh session should proceed normally.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...