Note that there are some explanatory texts on larger screens.

plurals
  1. POPython script run via cron does not execute occassionally
    text
    copied!<p>I have a simple python script for fetching tweets and caching them to disk that is configured to run every two minutes via cron.</p> <pre><code>*/2 * * * * (date ; /usr/bin/python /path/get_tweets.py) &gt;&gt; /path/log/get_tweets.log 2&gt;&amp;1 </code></pre> <p>The script runs successfully most of the time. However, every so often the script doesn't execute. In addition to other logging, I added a simple print statement above the meat of the script and nothing except the output from the initial date command makes it to the log. </p> <pre><code>#!/usr/bin/python # Script for Fetching Tweets and then storing them as an HTML snippet for inclusion using SSI print "Starting get_tweets.py" import simplejson as json import urllib2 import httplib import re import calendar import codecs import os import rfc822 from datetime import datetime import time import sys import pprint debug = True now = datetime.today() template = u'&lt;p class="tweet"&gt;%s &lt;span class="date"&gt;on %s&lt;/span&gt;&lt;/p&gt;' html_snippet = u'' timelineUrl = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=gcorne&amp;count=7' tweetFilePath = '/path/server-generated-includes/tweets.html' if(debug): print "[%s] Fetching tweets from %s." % (now, timelineUrl) def getTweets(): request = urllib2.Request(timelineUrl) opener = urllib2.build_opener() try: tweets = opener.open(request) except: print "[%s] HTTP Request %s failed." % (now, timelineUrl) exitScript() tweets = tweets.read() return tweets def exitScript(): print "[%s] Script failed." % (now) sys.exit(0) tweets = getTweets() now = datetime.today() if(debug): print "[%s] Tweets retrieved." % (now) tweets = json.loads(tweets) for tweet in tweets: text = tweet['text'] + ' ' when = tweet['created_at'] when = re.match(r'(\w+\s){3}', when).group(0).rstrip() # print GetRelativeCreatedAt(when) # convert links text = re.sub(r'(http://.*?)\s', r'&lt;a href="\1"&gt;\1&lt;/a&gt;', text).rstrip() #convert hashtags text = re.sub(r'#(\w+)', r'&lt;a href="http://www.twitter.com/search/?q=%23\1"&gt;#\1&lt;/a&gt;', text) # convert @ replies text = re.sub(r'@(\w+)', r'@&lt;a href="http://www.twitter.com/\1"&gt;\1&lt;/a&gt;', text) html_snippet += template % (text, when) + "\n" #print html_snippet now = datetime.today() if(debug): print "[%s] Opening file %s." % (now, tweetFilePath) try: file = codecs.open(tweetFilePath, 'w', 'utf_8') except: print "[%s] File %s cound not be opened." % (now, tweetFilePath) exitScript() now = datetime.today() if(debug): print "[%s] Writing %s to disk." % (now, tweetFilePath) file.write(html_snippet) now = datetime.today() if(debug): print "[%s] Finished writing %s to disk." % (now, tweetFilePath) file.close() sys.exit(0) </code></pre> <p>Any ideas? The system is a VPS running Centos 5.3 with python 2.4. </p> <p><strong>Update:</strong> I have added the entire script to avoid any confusion.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload