Python paths and Cron logging

I created two new twitter accounts yesterday and the amount of followers in such a short time is great to see. Feel free to follow them here – @bgp4_table and @bgp6_table

The accounts get updated through Python, and that Python script is run via a cron job once every six hours.

I noticed that when I ran my script manually, it worked fine. When the cronjob ran it, nothing happened. As there is no console log it made me wonder what the issue was.

Cron log

The first thing I needed to do was configure cron to log it’s output. I’ve done it like so in my crontab:

0 0,6,12,18 * * * /home/scripts/tweet.py > /home/scripts/tweet.log 2>&1

This directs all 1 and 2 output to the log file I created. In case you not aware, standard stream 0 is input, stream 1 is output, and stream 2 is errors. The commands above ensure I’m logging both output, if any, and errors, if any.

Python paths

On the next cron run, my log was created and it was plain to see:

$ less tweet.log
Traceback (most recent call last):
  File "/home/scripts/tweet.py", line 10, in 
    with open("v4_count") as f:
IOError: [Errno 2] No such file or directory: 'v4_count'

As part of my script, I save the last values in text files located in the same path as the script. When the script runs again, it reads the last value, get the new value, then works out a delta to display. I then write the latest value into that file for the next run. This is an example of one of those reads:

# Pull last delta
with open("v4_count") as f:
    old_v4 = int(f.readline())

This ran fine if I ran the script manually, but I was always running the script from within the same folder. This meant that python was finding and opening the file in the same folder. With the cronjob, it was getting called from somwehere else where v4_count did not exist.

The simple fix for this was to change all references to the full path:

# Pull last delta
with open("/home/scripts/v4_count") as f:
    old_v4 = int(f.readline())

This time on the next run, no more problems :)

© 2009-2018 Darren O'Connor All Rights Reserved -- Copyright notice by Blog Copyright