每日cron任务因超时而终止
我不知道为什么我的定时任务(cron)运行了20分钟后就被终止了。在openshift上,如果你运行定时任务,它会在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
但这可能和代码无关,因为如果我直接运行我的定时任务文件(./update_dave_list
),它会在20秒内完成,而通过cron运行时却无法完成。你觉得这里可能是什么问题呢?
编辑1:
所以我尝试把文件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上的每日定时任务文件夹看起来是这样的:
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列表更新后,这是来自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上得不到支持,报告错误也没有用(我几周前报告过一个,但到现在还没有更新)。很奇怪的是我的每日定时任务不工作,而每小时的定时任务却正常。
编辑2: 我尝试重启定时任务组件:
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
__________________________________________________________________________
我现在不确定该怎么办。如果每日定时任务中的文件被启动,它看起来是这样的:
__________________________________________________________________________
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:
...
但每日定时任务并没有运行每日目录中的任何文件。
相关文章:
- 暂无相关问题
2 个回答
因为我的信誉值不够,不能直接评论,所以我只好把这个当作回答来写。
顺便问一下,你确定 urlopen()
能在 OpenShift 上获取到 xls 文件吗?可能是防火墙或者其他什么东西阻止了这个请求。你应该在代码里加更多的日志信息。
好的,我想我找到了问题所在。
首先,我可以这样强制每天运行定时任务:
/usr/libexec/openshift/cartridges/cron/bin/cron_runjobs.sh daily
这是我每天定时任务的日志:
__________________________________________________________________________
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
我发现日志中打印的日期是 -> update_dave_list
已经成功完成!(Wed Nov 25 03:24:38 EST 2015
) 所以问题一定出在 openshift 上,它没有结束 update_dave_list
进程,或者是另一个文件被正确处理了,但它的日志是在成功完成后才写入的(这从未发生过)。第二个想法更合理。我有一个文件,在我的本地计算机上可以在 20 分钟内完成,但在 openshift 上却没有按时完成。我通过将这个长时间运行的定时任务拆分成多个部分(每小时一个定时任务)来解决这个问题,现在看起来运行得不错。
谢谢你的帮助!我明白这里不是很多人使用 Openshift,没有很多日志和信息,调试起来确实不容易!再次感谢你的时间和帮助!