每日cron运行终止,因为它超过了最大运行时间

时间:2015-11-19 08:57:52

标签: python django cron openshift

我不知道为什么我的cron运行了20分钟并且终止了。在openshift中,如果你运行cron,它会在5分钟后被杀死。如果你用nohup跑,它会在20分钟后被杀死 这是我在cron_daily上的错误日志。文件update_dave_list正在运行20分钟:

Thu Nov 19 03:08:08 EST 2015: START daily cron run
__________________________________________________________________________
/var/lib/openshift/55a000094/app-root/runtime/repo//.openshift/cron/daily/update_dave_list:
WARNING:py.warnings:/var/lib/openshift/55a0000094/python/virtenv/lib/python2.7/site-packages/django_crontab-0.6.0-py2.7.egg/django_crontab/crontab.py:13: RemovedInDjango19Warning: django.uti$
  from django.utils.importlib import import_module
/usr/libexec/openshift/cartridges/cron/bin/cron_runjobs.sh: line 114: 181616 Killed                  $executor "$SCRIPTS_DIR"
Warning: daily cron run terminated as it exceeded max run time
__________________________________________________________________________
Thu Nov 19 03:28:09 EST 2015: END daily cron run - status=137
__________________________________________________________________________

问题是update_dave_list只需要约20秒就可以运行: update_dave_list内容:

#!/bin/bash
date
nohup /var/lib/openshift/55000094/python/virtenv/bin/python /var/lib/openshift/55000094/app-root/runtime/repo/wsgi/digrin/manage.py crontab run 18e10bf4fb745d8a480230a3 # update dave lists
date

输出:

Št nov 19 03:43:47 EST 2015
nohup: ignoring input and appending output to `nohup.out'
Št nov 19 03:44:05 EST 2015

问题还在于,如果一个人被杀,我的其他日常生活都不会被运行。 哦,这是代码,以防你想知道我跑了20秒:

def update_dave_list():
    # if settings.ON_OPENSHIFT:
    response = urlopen("http://www.dripinvesting.org/tools/U.S.DividendChampions.xls")
    excell = xlrd.open_workbook(file_contents=response.read())
    for sheet in range(0,3): #for champions, challengers and contenders
        worksheet = excell.sheet_by_index(sheet)
        list, created = List.objects.get_or_create(name=worksheet.name, short_name=worksheet.name,
                                                   source="http://www.dripinvesting.org/tools/tools.asp")
        num_rows = worksheet.nrows - 1
        curr_row = 5
        symbol_list = []
        while curr_row < num_rows:
            curr_row += 1
            symbol = worksheet.cell(curr_row, 1)
            if symbol.ctype != 1: #break if you are out of symbol
                break
                #print  cell.value.replace(".", "-")
            symbol_list.append(unify_symbol(symbol.value))
            industry = worksheet.cell(curr_row, 2)
            sector = worksheet.cell(curr_row, 77)
            years = worksheet.cell(curr_row, 3)
            try:
                list_stock, created = ListStock.objects.get_or_create(stock=Stock.objects.get(symbol=unify_symbol(symbol.value)))
            except ObjectDoesNotExist:
                AddStock.objects.get_or_create(symbol=unify_symbol(symbol.value))
                continue
            list_stock.industry = industry.value
            list_stock.years_paying = years.value
            if sector.ctype == 1:
                if list_stock.stock.sector == None:
                    sector_obj, created = Sector.objects.get_or_create(name=sector.value)
                    list_stock.stock.sector = sector_obj
                    list_stock.stock.save()
            list_stock.save()
            #add stocks to list
            list.stocks.add(list_stock)
        #delete removed stocks
        for list_stock in list.stocks.all():
            if list_stock.stock.symbol not in symbol_list:
                list.stocks.remove(list_stock)
    return

但它可能不依赖于代码,因为如果我运行我的cron文件(./update_dave_list)它会在20秒内完成,当由cron运行时它将无法完成。知道这里可能有什么问题吗?

EDIT1: 所以我试着像这样评论文件update_dave_list

#!/bin/bash
#/var/lib/openshift/55a0310e4382ec4b84000094/python/virtenv/bin/python /var/lib/openshift/55a0310e4382ec4b84000094/app-root/runtime/repo/wsgi/digrin/manage.py crontab run 18e10bf4fb92741b69745d8a480230a3 # update dave lists

openshift上的每日cron文件夹如下所示:

drwx------. 2 55a00094 55a00094 4096 nov 22 17:55 .
drwx------. 7 55a00094 55a00094 4096 nov 22 17:39 ..
-rw-------. 1 55a00094 55a00094    0 nov 22 17:37 .gitignore
-rwxr-xr-x. 1 55a00094 55a00094  236 nov 22 17:37 update_dave_list
-rwxr-xr-x. 1 55a00094 55a00094  235 nov 22 17:37 update_dgr
-rwxr-xr-x. 1 55a00094 55a00094  244 nov 22 17:37 update_ex_dividends
-rwxr-xr-x. 1 55a00094 55a00094  250 nov 22 17:37 update_frequency
-rwxr-xr-x. 1 55a00094 55a00094  236 nov 22 17:37 update_industry
-rwxr-xr-x. 1 55a00094 55a00094  472 nov 22 17:37 update_stocks
-rwxr-xr-x. 1 55a00094 55a00094  243 nov 22 17:37 update_years_paying
-rwxr-xr-x. 1 55a00094 55a00094  252 nov 22 17:37 watcher

在我评论出dave list update之后,这是来自 cron_daily.log 的日志:

__________________________________________________________________________
Mon Nov 23 03:25:54 EST 2015: START daily cron run
__________________________________________________________________________
/usr/libexec/openshift/cartridges/cron/bin/cron_runjobs.sh: line 114: 28445 Killed                  $executor "$SCRIPTS_DIR"
Warning: daily cron run terminated as it exceeded max run time
__________________________________________________________________________
Mon Nov 23 03:45:54 EST 2015: END daily cron run - status=137
__________________________________________________________________________

我认为这意味着我的文件都没有运行,错误发生在cron_runjobs.sh中。但我没有得到openshift的支持,报告错误没有帮助(几周前我报告过,还没有更新)。奇怪的是,我的每日crons不起作用,每小时cron工作正常。

EDIT2: 我试图重新启动cron cartridge:

rhc cartridge remove cron -a <app>
rhc cartridge add cron -a <app>

没有帮助:

__________________________________________________________________________
Tue Nov 24 03:12:47 EST 2015: START daily cron run
__________________________________________________________________________
/usr/libexec/openshift/cartridges/cron/bin/cron_runjobs.sh: line 114: 211644 Killed                  $executor "$SCRIPTS_DIR"
Warning: daily cron run terminated as it exceeded max run time
__________________________________________________________________________
Tue Nov 24 03:32:47 EST 2015: END daily cron run - status=137
__________________________________________________________________________

我不知道现在该做什么。如果启动每日cron中的文件,它看起来像这样:

__________________________________________________________________________
Mon Nov 23 13:01:05 EST 2015: START hourly cron run
__________________________________________________________________________
/var/lib/openshift/55a000094/app-root/runtime/repo//.openshift/cron/hourly/add_stocks:
...

但是每日cron不会在日常目录中运行任何文件。

2 个答案:

答案 0 :(得分:5)

好的,我想我发现了问题所在。
首先,我可以强迫每天运行这样的cron:

/usr/libexec/openshift/cartridges/cron/bin/cron_runjobs.sh daily

这是我每日cron的日志:

__________________________________________________________________________
Wed Nov 25 03:24:19 EST 2015: START daily cron run
__________________________________________________________________________
/var/lib/openshift/55a000094/app-root/runtime/repo//.openshift/cron/daily/update_dave_list:
WARNING:py.warnings:/var/lib/openshift/55a000094/python/virtenv/lib/python2.7/site-packages/django_crontab-0.6.0-py2.7.egg/django_crontab/crontab.py:13: RemovedInDjango19Warning: django.uti$
  from django.utils.importlib import import_module
Wed Nov 25 03:24:38 EST 2015
/usr/libexec/openshift/cartridges/cron/bin/cron_runjobs.sh: line 114: 375681 Killed                  $executor "$SCRIPTS_DIR"
Warning: daily cron run terminated as it exceeded max run time

我认为问题出在update_dave_list上。但是当我在update_dave_list文件中打印日期时,这样:

#!/bin/bash
/var/lib/openshift/55a000094/python/virtenv/bin/python /var/lib/openshift/55a000094/app-root/runtime/repo/wsgi/digrin/manage.py crontab run 18e10bf4fb92741b69745d8a480230a3 # update dave lists
date

我意识到日期是在日志中打印的 - &gt; update_dave_list 已成功完成! (Wed Nov 25 03:24:38 EST 2015)所以问题必须在openshift中,不会结束update_dave_list进程或正确选择另一个文件,但是在成功完成后会将其写入日志(从未发生过)。第二个想法更合乎逻辑。我有一个文件,这是在我的localhost上20分钟内完成的,但是在openshift上它没有准时完成。我通过将这个长时间运行的cron分成多个部分(每小时cron)来解决这个问题,现在看起来它运行正常。
感谢帮助!我知道这里没有多少人使用Openshift,没有很多日志和信息,调试也不是一件容易的事!再次感谢您的时间和帮助!

答案 1 :(得分:1)

没有足够的声誉来添加评论,所以我必须添加一个答案作为评论。

以防万一,您确定urlopen()可以在OpenShift上获取xls吗?也许防火墙或其他东西阻止了这个请求。您应该在代码中添加更多日志。