【问题标题】:Python script run via cron does not execute occassionally通过 cron 运行的 Python 脚本偶尔不会执行
【发布时间】:2012-07-14 08:50:37
【问题描述】:

我有一个简单的 python 脚本,用于获取推文并将它们缓存到磁盘,该脚本配置为通过 cron 每两分钟运行一次。

*/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)

有什么想法吗?该系统是一个运行 Centos 5.3 和 python 2.4 的 VPS。

更新:我添加了整个脚本以避免任何混淆。

【问题讨论】:

    标签: python cron


    【解决方案1】:

    最可能的解释是,有时脚本需要超过两分钟(可能系统偶尔很忙,或者脚本可能不得不等待某个偶尔繁忙的外部站点等),而您的 cron 是一个明智的跳过尚未终止的重复事件。通过记录脚本的开始和结束时间,您将能够仔细检查是否是这种情况。在这种情况下你想做什么取决于你(我建议你考虑跳过偶尔的运行以避免进一步超载一个非常繁忙的系统——你自己的,或者你从中获取数据的远程系统)。

    【讨论】:

    • 在整个 python 脚本的各个点都有日志记录,但在 crond 生成的进程挂起的情况下,不会记录或执行 python 脚本中的任何行。
    • @gcome,如果您没有记录上一次运行的确切开始和结束时间,则无法检查我的假设是否(即结束时间只是在其他计划的运行时间之后下一次运行)是正确的。
    • 我已经添加了整个脚本,它显示了我在哪里/如何记录脚本中执行的各种操作。在我对日志的分析中,最后一个事件(写入完成)的时间远远早于下一个 cron 开始时间的开始。尽管如此,我会看看你的建议是否有成果。
    【解决方案2】:

    我只是遇到了一个 Python 脚本问题,该脚本有时无法在 crontab 中运行,但总是从命令行运行。结果我不得不将日志重定向到/dev/null。否则标准输出似乎已满,程序停止,进程被终止。使用/dev/null 转储输出,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-27
      • 2020-08-16
      • 1970-01-01
      • 2014-09-06
      • 2019-07-23
      • 2013-10-11
      • 2020-12-26
      • 2015-04-12
      相关资源
      最近更新 更多