Home arrow Debugging Tips arrow Rsync is not working from crontab
Rsync is not working from crontab PDF
Thursday, 01 March 2012
If you want to backup files remotely using rsync and you've configured ssh authentication via public/private keys and your script from command line works but after you try to run it from a contrab file located in /var/cron/tabs, the problem might be that you copied a crontab example from /etc/crontab which has an extra column.

So instead of having:

*/5 *   *   *   *   john /home/john/scripts/sync.sh

just use:

*/5 *   *   *   *   /home/john/scripts/sync.sh

in your /var/cron/tabs/john file.

Here is an example of running rsync to backup a public_html directory from homedir to an external machine:

sync.sh
#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin
export PATH

cd /home/john/public_html/
/usr/local/bin/rsync -e '/usr/bin/ssh -p 22' -i --bwlimit=200 --exclude-from 'exclude.txt' -avc  /home/john/public_html/  This e-mail address is being protected from spam bots, you need JavaScript enabled to view it :/usr/home/john/public_html/ >>/home/john/scripts/output.log 2>&1

echo $? >>/home/john/scripts/output.log 2>&1

Note1: For any command to run from crontab the best approach is to use full path since cron see only a limited path or to redefine path in your script.

In previous example PATH environment variable is not needed because we used complete path for rsync. Previous example uses SSH with auth keys, so you must configure SSH authentication that way, first.

A quick tutorial about SSH public key authentication can be found here: SSH public key authentication in FreeBSD.

Note2: If you modify your main crontab file /etc/crontab (which is not recomended to be modified, because those modifications could be lost when upgrading), or by creating a /var/cron/tab/username file (which is the right way to do it) you might notice cron might not reload your modifications soon enough (after a while it will reload crontab files). So to quickly reload crontab files restart cron daemon:

  /etc/rc.d/crond restart

You can always observe what cron service is running by looking into /var/log/cron file:

  tail -f /var/log/cron

Note3: When running a command from crontab you can send the output of the command via email. This is usefull for debugging:

  */5 *   *   *   *   john /home/john/scripts/sync.sh | mail -s "rsync script" This e-mail address is being protected from spam bots, you need JavaScript enabled to view it

Or you can place your whole command into a script and test for errors when running that command.

Last Updated ( Thursday, 01 March 2012 )
 
< Prev   Next >

Other BSD Systems

OpenBSD

Misc

Solaris

Polls

Best BSD firewall?