Rsync works in shell but not in cron

So I have this rsync script I wrote to grab some sql files and import them to a database. I left in the mysql stuff just give context to the situation. The real problem is with my rsync code.

script.sh (chmod 744)

#!/bin/sh
rsync -av --rsh="sshpass -p'PASSWORD' ssh -l'USERNAME'" WEBSITE.com:/PATH/TO/RSYNC/REMOTE /PATH/TO/RSYNC/LOCAL
tables=(TABLE1 TABLE2)
for name in ${tables[@]}
do
mysql -u'USER' -p'PASSWORD' DBNAME < PATH/TO/SQLFILES/$name.sql
done

My cron looks like this ...

0 * * * * /PATH/TO/SCRIPT/script.sh

Here's the problem. When I execute the script in bash, it works fine. But my cron job returns these errors

rsync: Failed to exec sshpass: No such file or directory (2)
rsync error: error in IPC code (code 14) at pipe.c(84) [receiver=3.0.6]
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [receiver=3.0.6]

I don't get it, why would the script work in bash but not in cron ? What am I doing wrong ? :wall:

Thanks :smiley:

Hi,
May you try to reference the full path to sshpass in the 2nd line? (you can use

which sshpass

to find tghe full path).

rsync -av --rsh="/path/to/sshpass -p'PASSWORD' ssh -l'USERNAME'" WEBSITE.com:/PATH/TO/RSYNC/REMOTE /PATH/TO/RSYNC/LOCAL

Let us know how it goes
see ya
fra

That worked ! Thanks frappa !

Glad to hear that!

The hint to the problem root cause was given by:

rsync: Failed to exec sshpass: No such file or directory (2)

The explanation should be that, when executed from crontab, the environment initialization files (.bashrc, .bash_profile, etc) are not read, so that the PATH variable is not initialized with the path of the sshpass executable.

see ya
fra