1

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.

Sparctus
  • 33
  • 1
  • 6
  • 1
    Have you tried the absolute path to `scp` (i.e.: `/usr/bin/scp`) or `/tmp`?. What user do you run the cron as? What's the working directory of the crons? What's in test.log? – netcoder Nov 24 '11 at 01:24
  • And an adjunct to @netcoder's question - what is in `/dev/null`? Since it won't tell you, I suggest you capture the standard output and standard error in a file so you can see whether the information you need is in there. For example, how does `scp` know which password to use? – Jonathan Leffler Nov 24 '11 at 01:27

2 Answers2

1

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.

Community
  • 1
  • 1
sarnold
  • 102,305
  • 22
  • 181
  • 238
1

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?

Community
  • 1
  • 1
Lee Netherton
  • 21,347
  • 12
  • 68
  • 102