Python script won't write data when ran from cron

Solution 1:

  • Check log files grep -i cron /var/log/syslog
  • Add an empty line to the end of the crontab, This has been a known bug for ages, not sure if it is solved.
  • Remove the 2>&1 from the command line until it works as designed. Any usefull errors are redirected to a file that is not created; effectively lost.
  • Check if root received mail (eg. using mutt or in /var/spool/mail). Error messages from cron are sent to system email by default.

Also:

  • Reconsider the 777 permissions as soon as possible. When running from root, 755 root:root should be sufficient to be able to check the logs from unprivileged user)
  • Reconsider running the script from root, it is bad practise.

Solution 2:

What works for me

Crontab

#Borrowed from anacron
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#End borrowed from anacron

* * * * *   python /home/username/somedir/test.py

Python script

scheduleUrl = 'http://example.com/index.html'
schedule = '/tmp/test.html'

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

# Download the schedule
wget(scheduleUrl, schedule)

Added environment variables. Used tmp instead of var to verify there weren't permissions issues.

Solution 3:

I had a similar problem:

f = open('./my_file.txt', 'w')
f.close()

Was not opening and writing the file when run from cron. This solved it

f = open('<full_path_of_file>/my_file.txt', 'w')
f.close()