Question about cronjob

Dear all,

I wrote a script and set it as a cron job to automatically download the data from the website.

Here is the problem, when I run this script in terminal, it works well, while it can not download the files when I run it as a cron job, I feel so weird.

The script as follows:

v#!/bin/csh
# name: auto_download_all
# Purpose: automatically download, change directory and reformat for all stations
# Output: txt and csv -formatted file

set directory1 = '/data/gipsy/bin/chen/nwcc_web_report_scripting'
set directory2 = '/data/gipsy/bin/chen'
set directory3 = '/usr/local/apache/htdocs/i/snow/in_situ/by_product/snotel'

set gps_num = `cat /gipsy/files/gps_snotel2.txt | awk '{if ($1 != "#" && $4 <25000) print substr($1,1,4)}' | tr '[A-Z]' '[a-z]'`
set snotel_num = `cat /gipsy/files/gps_snotel2.txt | awk '{if ($1 != "#" && $4 <25000) print substr($3,1,3)}' | tr '[A-Z]' '[a-z]'`


foreach site_num($gps_num)
  set snotel = `snotelID $site_num`
    #download file
    $directory1/gethistoric.sh $snotel ALL Daily 2012 CY 
    # move the file to a new dir
    mv $directory2/${snotel}-2012CY.csv    $directory3/${site_num}/${snotel}-2012CY.csv    
end

Do you guys have any solutions about it? Thank you very much!

What is the error you are getting?...

Try sourcing the profile at the beginning . ~/.profile

--ahamed

Thanks,

The problem is when I run this script in cronjob, it can not download the files from the website while it can when I run it in terminal.

Thanks again!

---------- Post updated at 08:37 PM ---------- Previous update was at 08:36 PM ----------

Thanks,

The problem is when I run this script in cronjob, it can not download the files from the website while it can when I run it in terminal.

Thanks again!

Did you try sourcing the profile?

Put a set -x in the script and in the crontab redirect the stout and sterr to some log file, it will help us in identifying the issue

Something like this in the crontab

* * * * /your/script >/tmp/log 2>&1

--ahamed

Could you please kindly tell me what should I put '. ~/.profile ' and 'set -x' in the script? Could you just give me the full edition of the correct script? I am sorry I am new here, thank you very much.

Do this in your crontab and check the log file

* * * * /bin/csh -x /your/script >/tmp/log 2>&1

--ahamed

---------- Post updated at 07:50 PM ---------- Previous update was at 07:46 PM ----------

Try sourcing the profile like this...

#!/bin/csh
source ~/.profile
...

I see a "v" in the code you have pasted, assume its a typo from ctrl+v

--ahamed

Thanks, where is the crontab file?

First try the sourcing and see if it works.

For editing crontab, use crontab -e

--ahamed

How to save the edit and quit when I finished?

Thanks

That depends on the editor you are using... if "VIM" or "vi" is the editor, issue the command ":wq!"

--ahamed

When I input as you told me, it asked: 'Do you want to retry the same edit? ' And I said: no, then it said: 'crontab: edits left in /tmp/crontab.XXXXF72RjH'

But when I reopen the crontab by crontab -e, it doesn't changed. And at the bottom of the terminal , it said: '"/tmp/crontab.XXXXLXEy6w" 3L, 142C'

What is the problem of this?

Thank you very much.

Which is the editor you are using?

--ahamed

vi, I am not familiar with it, how can I edit by gedit or other editor?

Try this...

crontab -l > tmpfile
gedit tmpfile
crontab < tmpfile

--ahamed

---------- Post updated at 08:26 PM ---------- Previous update was at 08:22 PM ----------

BTW, did you try sourcing the profile?

--ahamed

Yes, I treid both of these but failed, after I changed it in gedit, I found it doesn't changed in crontab -e. Could you tell me why?

Thanks

After editing the tmpfile did you load it using crontab < tmpfile ?

--ahamed

But it also doesn't work..........

That is odd!

Try cat tmpfile | crontab -

Also, please paste the output of crontab -l and cat tmpfile

And did you try sourcing the profile in the script?

--ahamed

Yes, I have tried.

h4:~>cat tmpfile | crontab -
"-":4: bad day-of-week
errors in crontab file, can't install.

What is the problem?

Output of cat tmpfile ?

--ahamed