Python script won't write data when ran from cron

Posted by Ruud on Ask Ubuntu See other posts from Ask Ubuntu or by Ruud
Published on 2011-11-28T21:47:11Z Indexed on 2012/04/08 5:46 UTC
Read the original article Hit count: 455

Filed under:
|

When I run a python script in a terminal it runs as expected; downloads file and saves it in the desired spot.

sudo python script.py    

I've added the python script to the root crontab, but then it runs as it is supposed to except it does not write the file.

$ sudo crontab -l
> * * * * * python /home/test/script.py >> /var/log/test.log 2>&1

Below is a simplified script that still has the problem:

#!/usr/bin/python

scheduleUrl = 'http://test.com/schedule.xml'
schedule = '/var/test/schedule.xml'

# Download url and save as filename
def wget(url, filename):
    import urllib2
    try:
        response = urllib2.urlopen(url)
    except Exception:
        import traceback
        logging.exception('generic exception: ' + traceback.format_exc())
    else:
        print('writing:'+filename+';')
        output = open(filename,'wb')
        output.write(response.read())
        output.close()

# Download the schedule
wget(scheduleUrl, schedule)

I do get the message "writing:name of file;" inside the log, to which the cron entry outputs. But the actual file is nowhere to be found...

The dir /var/test is chmodded to 777 and using whatever user, I am allowed to add and change files as I please.

© Ask Ubuntu or respective owner

Related posts about python

Related posts about script