Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have been stuck with a peculiar problem, where rsync command is not running when it is executed through crontab.
Below is the code :

#!/bin/sh -x
/bin/sh /etc/profile
MyUSER="root"       # USERNAME
MyPASS="password"         # PASSWORD
MyHOST="localhost"  # Hostname
Password="" #Linux Password

MYSQL="$(which mysql)"
if [ -z "$MYSQL" ]; then
echo "Error: MYSQL not found"
exit 1
MYSQLADMIN="$(which mysqladmin)"
if [ -z "$MYSQLADMIN" ]; then
    echo "Error: MYSQLADMIN not found"
    exit 1
CHOWN="$(which chown)"
if [ -z "$CHOWN" ]; then
    echo "Error: CHOWN not found"
    exit 1
CHMOD="$(which chmod)"
if [ -z "$CHMOD" ]; then
    echo "Error: CHMOD not found"
    exit 1

GZIP="$(which gzip)"
if [ -z "$GZIP" ]; then
    echo "Error: GZIP not found"
    exit 1
CP="$(which cp)"
if [ -z "$CP" ]; then
    echo "Error: CP not found"
    exit 1
MV="$(which mv)"
if [ -z "$MV" ]; then
    echo "Error: MV not found"
    exit 1
RM="$(which rm)"
if [ -z "$RM" ]; then
    echo "Error: RM not found"
    exit 1
RSYNC="$(which rsync)"
if [ -z "$RSYNC" ]; then
    echo "Error: RSYNC not found"
    exit 1

MYSQLBINLOG="$(which mysqlbinlog)"
if [ -z "$MYSQLBINLOG" ]; then
    echo "Error: MYSQLBINLOG not found"
    exit 1
# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y-%T")"

mkdir $DEST/Increment_backup.$NOW
$MYSQLADMIN -u$MyUSER -p$MyPASS flush-logs
newestlog=`ls -d /usr/local/mysql/data/mysql-bin.?????? | sed 's/^.*\.//' | sort -g | tail -n 1`
echo $newestlog
for file in `ls /usr/local/mysql/data/mysql-bin.??????`
        if [ "/usr/local/mysql/data/mysql-bin.$newestlog" != "$file" ]; then
     echo $file
     echo $Password | sudo -S $CHMOD 0777 $file
         #sudo $MYSQLBINLOG $file>$file.$NOW.sql
     $CP "$file" $LATEST
     #$RM "$file.$NOW.sql"
     #$MV $file.sql.gz /$LATEST
for file1 in `ls $LATEST/mysql-bin.??????`
 $MYSQLBINLOG $file1>$file1.$NOW.sql 
 $GZIP -9 "$file1.$NOW.sql"     
 $RM "$file1"
 $RSYNC -v -e ssh $LATEST abc@
#$GZIP -f "$FILE.$NOW.sql"

Rsync happens when the code is run manually, but fails when it is run through crontab. Rest of the commands are working fine. From the logs I got this information:

Host key verification failed.^M
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(600) [sender=3.0.6]
share|improve this question
It can be a permission problem. Are you running the cron job under same user when running the script manually?? – Khaled Jan 31 '13 at 12:20
Thank you Khaled. Im running the script as a root user. – Rudra Jan 31 '13 at 12:24
What means "fails"? Can you show exact output in that case? – gelraen Jan 31 '13 at 12:39
Thanks for the reply Gelraen. Fails means the script is running fine when executed manually, but in crontab nothing happens, no transfer of files, etc. – Rudra Jan 31 '13 at 12:40
Please show us your crontab entry. Additionally make sure you have error logging there also like this: ' >>/tmp/logfile 2>&1' – grassroot Jan 31 '13 at 14:15

ssh (which rsync executes) doesn't trust the host key of the server, and since there is no interactive terminal to prompt the user it simply fails. The cause is probably that ssh is looking at a different known_hosts file when running under cron that when you execute it manually.

If the cronjob runs as a different user to you, you need to manually accept the host key by doing something like this:

sudo -u cronuser HOME=/home/cronuser ssh abc@

If the cronjob is running as your user then $HOME probably isn't set correctly. Specify that explicitly in the crontab:

share|improve this answer
Thanks for the reply mgorven. I'm running this script as a root. The script runs with rsync when executed manually, but is failing on crontab, should I fix this with some environment variable. – Rudra Feb 1 '13 at 7:12
You could add the option -o StrictHostKeyChecking=no to the ssh command in your script. – Jenny D Feb 1 '13 at 10:40
@Vishu Try setting HOME=/root in the crontab. Are there any prompts about SSH when running the script manually? – mgorven Feb 1 '13 at 17:47
up vote 0 down vote accepted

Thank you the answer finally. When I was running it manually I was running as a local user. But, on cron I was running it as root user, for which ssh key was not generated. I did ssh-keygen and created the keys. Now it works.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.