2010-05-06 93 views
2

我有一個簡單的python腳本來獲取推文並將它們緩存到配置爲每隔兩分鐘通過cron運行的磁盤。通過cron運行的Python腳本不會偶爾執行

*/2 * * * * (date ; /usr/bin/python /path/get_tweets.py) >> /path/log/get_tweets.log 2>&1 

該腳本大部分時間都能成功運行。然而,腳本經常不執行。除了其他日誌記錄之外,我在腳本之上添加了一個簡單的打印語句,除了從初始日期命令輸出到日誌之外,沒有任何內容。

#!/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'<p class="tweet">%s <span class="date">on %s</span></p>' 
html_snippet = u'' 
timelineUrl = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=gcorne&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'<a href="\1">\1</a>', text).rstrip() 
    #convert hashtags 
    text = re.sub(r'#(\w+)', r'<a href="http://www.twitter.com/search/?q=%23\1">#\1</a>', text) 
    # convert @ replies 
    text = re.sub(r'@(\w+)', r'@<a href="http://www.twitter.com/\1">\1</a>', 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) 

任何想法?該系統是一個使用python 2.4運行Centos 5.3的VPS。

更新:我已經添加了整個腳本以避免混淆。

回答

2

最可能的解釋是,偶爾腳本需要兩分鐘以上的時間(也許系統偶爾會很忙,或者腳本可能需要等待某些偶爾會遇忙的外部站點等),並且您的cron的a明智的,可以跳過尚未終止的重複事件。通過記錄腳本的起始和結尾時間,您可以仔細檢查是否屬於這種情況。在這種情況下你想要做的事情取決於你(我建議你考慮跳過偶爾的跑步以避免進一步超載一個非常繁忙的系統 - 你自己的或者你從中獲取數據的遠程系統)。

+0

在各個點上都存在整個python腳本的日誌記錄,但在由crond產生的進程掛起的實例中,沒有任何來自python腳本的行被記錄或執行。 – gcorne 2010-05-07 01:22:23

+0

@gcome,如果您沒有記錄上一次運行的確切開始和結束時間,則無法檢查我的假設(即結束時間是在下一次運行的其他計劃運行時間之後)是否正確。 – 2010-05-07 03:27:01

+0

我已經添加了整個腳本,它顯示了我在何處/如何記錄腳本中採取的各種操作。在我對日誌的分析中,最後一個事件的時間(寫入完成)在下一個cron開始時間的開始之前完成。儘管如此,我會看看你的建議是否成功。 – gcorne 2010-05-07 22:49:29

1

我剛剛遇到了一個Python腳本的問題,它有時不會在crontab中運行,但總是從命令行運行。原來我不得不將日誌重定向到/dev/null。標準輸出似乎已經滿了,程序剛好停止,進程終止。使用/dev/null轉儲輸出,一切都很好。