Reputation: 43
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 [email protected]:/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 [email protected]
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 [email protected], just to make sure that cron uses john's account on mysite.com when crond runs the script. Still, same result.
Upvotes: 2
Views: 2143
Reputation: 22562
It may be a problem with your $USER
environment variable not being set when run under cron.
You could try adding something like this to your script:
echo "User: $USER" > /tmp/crontest.log
After getting cron to run the script have a look at what is in /tmp/crontest.log
If nothing is being set you might want to try something like this: Where can I set environment variables that crontab will use?
Upvotes: 1
Reputation: 104120
I expect the problem is right here: mysite.com:tmp/$USER
-- tmp/
is a relative path, relative to the current working directory. When your code is executed via crond(8)
, your cwd
might be different than when you execute it by hand.
As @netcoder points out in his comment, absolute paths are the best way to work with scripts / programs executed out of crontab(5)
files.
Upvotes: 1