Linux – why rsync is not working with crontab

amazon ec2centoslinuxrsyncssh

when I run the following manually it runs perfectly:

/usr/bin/rsync -arvth -e "ssh -i /watch/scripts/word.pem" 1.1.1.1:/opt/ /opt

when it runs using crontab. I get:

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: unexplained error (code 255) at io.c(600) [receiver=3.0.6]

what am I missing here?

Thanks!
Dotan.

Best Answer

Cron does not run in the same environment (with the same environment variables) as you do in an interactive login shell. I would guess it has something todo with that.

You probably either have an ssh-agent at your local computer which you forward to get access to 1.1.1.1 or you have something else set as an environment variable that you need to access the server.

You can set environment variables for cron by setting them in the cron file:

VARIABLE=true
0 * * * * /usr/bin/rsync -arvth -e "ssh -i /watch/scripts/word.pem" 1.1.1.1:/opt/ /opt
Related Topic