Edit: Updated to reflect some answers
I have this script, test.sh on my home computer:
Note: $USER = john
#!/bin/bash
/usr/bin/scp -q john@mysite.com:/home/$USER/tmp/$USER /home/$USER/tmp/ > /dev/null 2>&1
error_code="$?"
if [ "$error_code" != "0" ]; then #if file NOT present on mysite then:
echo "File does not exist."
exit
fi
echo "File exists."
Now, lets say I create the file on the server mysite.com like so:
echo > tmp/$USER
Now, when I run the above script on my desktop manually, like so:
./test.sh
I get the result "File exists."
But if I run it via crontab, I get the result "File does not exist" My crontab looks like this:
* * * * * /home/user/test.sh >> /home/user/test.log 2>&1
I've spent all day trying to check the logic and everything... I can't seem to figure out why this is so. Thanks for all your help in advance :)
Edit: scp looks in mysite.com:/home/$USER/tmp/ dir The $USER on my desktop and the server are same. So I don't think it's an issue of relativeness. If I were to
ssh $USER@mysite.com
and then do
ls tmp/
I'll see the file there.
Also, the crontab entry is in my crontab, not another users' or root's crontab.
@Jonathan: I've set up key based authentication. No password required!
@netcoder: In my log file, I see repeated lines of "File does not exist."
@sarnold: in my scp line, I've put john@mysite.com, just to make sure that cron uses john's account on mysite.com when crond runs the script. Still, same result.