I have a backup.sh file that launch an rsync command. This rsync is to backup my dedicated server on a RaspberryPi running raspbian.

(I use keychain so I don't need to type any password etc ...)

The problem is that when I launch the batch manually everything works, but when it's crontab (with the same user) I have the following error :

2013/10/07 19:36:02 [6456] rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
2013/10/07 19:36:02 [6456] rsync error: error in rsync protocol data stream (code 12) at io.c(605) [Receiver=3.0.9]

Here is my backup.sh file


echo "_ backup start "$(date +%H:%M:%S)
echo " "

/usr/bin/rsync -avh --rsync-path='/usr/bin/rsync' --delete --log-file='/home/user/rsync.'$(date +%d%m%Y-%H%M%S)'.log' --rsh='ssh -p 1234' user@server.com:/path/to/archives/ /media/backup/

echo " "
echo "_ backup end "$(date +%H:%M:%S)

And now the crontab line (crontab -e)

# m h  dom mon dow   command
 30 5  *   *   *     /home/user/backup.sh | mail -s "Backup RPi "$(date +\%d/\%m/\%Y-\%X) user+rpibackup@gmail.com



(I use keychain so I don't need to type any password etc ...)

Ok, so you need to tell the program running in your cron job how to find your keychain.

SSH looks for an SSH agent (which keychain emulates) via the environment variable SSH_AUTH_SOCK. So you need to set this environment variable in your crontab.

In a typical configuration, SSH_AUTH_SOCK is a path to a socket with a random name. Since you're using keychain, you can easily find an alternate name for that socket: keychain writes files to ~/.keychain that contain environment variable declarations that set SSH_AUTH_SOCK and other similar variables (SSH_AGENT_PID, GPG_AGENT_INFO). So just include the appropriate file in your cron job.

30 5  *   *   *     . ~/.keychain/$(hostname)-sh; /home/user/backup.sh

(Aside: cron has a built-in feature to send mail with the job's output, such that you receive a mail only if the job does produce some output. No need to fiddle with | mail.)

  • Thank you very much :) I discovered keychain only two days ago and I'm not very confortable with it. Anyway your answer solved my problem, so thank you very much again. And thank you for SSH mail tip but I'm using different email address something+crontask@gmail.com so I can easily label emails in Gmail. – mephissto Oct 8 '13 at 19:45

The ssh connection isn't working from the cronjob. How is it supposed to be performing SSH authentication? No attempt to list an identity key or password is visible. I'd stick a '-i' with the path to your (unpassworded...) key if thats how its working. The main differences between command line and cronjob tends to be down to the environment (both things like environment variables, the lack of a controlling terminal etc)


I think you have a quoting problem on your subject:

mail -s "Backup RPi "$(date +\%d/\%m/\%Y-\%X) user+rpibackup@gmail.com

Should be this:

mail -s "Backup RPi $(date +\%d/\%m/\%Y-\%X)" user+rpibackup@gmail.com

Alternative Approach

I would wrap the email into the script as well.


  echo "_ backup start "$(date +%H:%M:%S)
  echo " "

  /usr/bin/rsync -avh --rsync-path='/usr/bin/rsync' --delete \
     --log-file='/home/user/rsync.'$(date +%d%m%Y-%H%M%S)'.log' \
     --rsh='ssh -p 1234' user@server.com:/path/to/archives/ /media/backup/

  echo " "
  echo "_ backup end "$(date +%H:%M:%S)
) | mail -s "Backup RPi $(date +\%d/\%m/\%Y-\%X)" user+rpibackup@gmail.com

Then change the crontab entry to this:

30 5  *   *   *     /home/user/backup.sh

Also make sure that the account that is running this cron can in fact send email. Do a test.

  • It's working well, I've been using it like that for some time now. Thanks anyway ^^ – mephissto Oct 8 '13 at 19:46

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.