我的程序退出的原因
the reason for my program's exit
我有一个 python 脚本 运行 在后台与 nohup ./myprogram.py 1>console.out &
结合。该程序不断地记录到某个日志文件并且处理时间很长。 运行宁 2 天后(周六和周日)我看到
# myprogram and myprogram2 are both running in background
# myprogram2 clearly has finished
[1] + 25159 exit 1 nohup ./myprogram.py 1>console.out &
[2] + 25442 done nohup ./myprogram2.py 1>console2.out &
我的程序的日志
2016-05-27 16:55:06 - sources.edf - INFO - processing day 1 ...
2016-05-27 16:55:06 - sources.edf - INFO - processing day 2 ...
...
2016-05-27 16:55:06 - sources.edf - INFO - processing day n ...
并停止(应该有 n + 1 和更多)。
可悲的是,我已经在 console.out 上覆盖了(所以在我看之前转储覆盖它......但我从第 n 天开始继续,似乎程序可以 运行没有任何 error/exception)
我知道这个描述有点或太模糊,无法指出退出的任何原因。我只需要知道一些关于这个的线索。我对此并不陌生,但我确实缺乏经验。所以任何可能的猜测都是值得赞赏的。
简化的源代码:
import os
import sys
import logging
import logging.config as lconfig
from optparse import OptionParser
from contextlib import closing
from datetime import datetime, timedelta
from collections import defaultdict
import psycopg2
from configobj import ConfigObj
if __name__ == "__main__":
## setup optparser and parse argvs and return opts and args
conf = ConfigObj(opts.config, list_values=False)[args[0]]
__import__(conf["module"])
## myprogram and myprogram2 is running the same source code
## same module. only the data is different
## source will provide Mapper and iterator as APIs
source = sys.modules[conf["module"]]
## extract start and end date, configure log
# set up regions info and mapper
## connect to db and read countries and exchange list
with closing(psycopg2.connect(conf["dsn"])) as db:
cursor = db.cursor()
regions = source.setup_region(conf['Regions'], cursor)
## find all wanted exchanges: (exch, region)
exchanges = source.setup_exchanges(conf['Exchanges'], cursor)
mapper = source.Mapper(exchanges, cursor, conf)
iterator = source.iterator(conf, start, end)
logger = logging.getLogger()
logger.info('START')
with regions:
for filename, raw_rec in iterator:
logger.info('Processing file {0}'.format(filename)
try:
record = source.Record(filename, raw_rec)
except Exception as e:
logger.warn("record parsing error: %s" % e)
continue
stks = mapper.find(record)
if not stks:
continue
regs = defaultdict(set)
for stk in stks:
regnm = exchanges[stk[2]]
regs[regnm].add(stk)
for reg,secs in regs.iteritems():
info = regions[reg]
outf = info.get_file(record.get_tm())
source.output(outf, record, secs, info.tz, conf)
logger.info('END')
日志刚刚停止,因为正在处理某些文件...
数据库连接随时可能断开,您的代码不处理这个问题。此外,计算机确实需要不时重启,例如断电、安全更新。
显然,还有其他原因可能导致您的代码中断,这就是为什么您需要将其运行 作为一项服务以防万一您想让它变得可靠。
了解如何 运行 此 python 脚本作为系统服务并将此服务配置为在失败时自动重启。这将阻止它停止,即使存在导致它崩溃的错误。如果它确实崩溃,Systemd 将重新启动它。
有很多资源对此进行了记录,但您可以从 https://learn.adafruit.com/running-programs-automatically-on-your-tiny-computer/systemd-writing-and-enabling-a-service
开始
我有一个 python 脚本 运行 在后台与 nohup ./myprogram.py 1>console.out &
结合。该程序不断地记录到某个日志文件并且处理时间很长。 运行宁 2 天后(周六和周日)我看到
# myprogram and myprogram2 are both running in background
# myprogram2 clearly has finished
[1] + 25159 exit 1 nohup ./myprogram.py 1>console.out &
[2] + 25442 done nohup ./myprogram2.py 1>console2.out &
我的程序的日志
2016-05-27 16:55:06 - sources.edf - INFO - processing day 1 ...
2016-05-27 16:55:06 - sources.edf - INFO - processing day 2 ...
...
2016-05-27 16:55:06 - sources.edf - INFO - processing day n ...
并停止(应该有 n + 1 和更多)。
可悲的是,我已经在 console.out 上覆盖了(所以在我看之前转储覆盖它......但我从第 n 天开始继续,似乎程序可以 运行没有任何 error/exception)
我知道这个描述有点或太模糊,无法指出退出的任何原因。我只需要知道一些关于这个的线索。我对此并不陌生,但我确实缺乏经验。所以任何可能的猜测都是值得赞赏的。
简化的源代码:
import os
import sys
import logging
import logging.config as lconfig
from optparse import OptionParser
from contextlib import closing
from datetime import datetime, timedelta
from collections import defaultdict
import psycopg2
from configobj import ConfigObj
if __name__ == "__main__":
## setup optparser and parse argvs and return opts and args
conf = ConfigObj(opts.config, list_values=False)[args[0]]
__import__(conf["module"])
## myprogram and myprogram2 is running the same source code
## same module. only the data is different
## source will provide Mapper and iterator as APIs
source = sys.modules[conf["module"]]
## extract start and end date, configure log
# set up regions info and mapper
## connect to db and read countries and exchange list
with closing(psycopg2.connect(conf["dsn"])) as db:
cursor = db.cursor()
regions = source.setup_region(conf['Regions'], cursor)
## find all wanted exchanges: (exch, region)
exchanges = source.setup_exchanges(conf['Exchanges'], cursor)
mapper = source.Mapper(exchanges, cursor, conf)
iterator = source.iterator(conf, start, end)
logger = logging.getLogger()
logger.info('START')
with regions:
for filename, raw_rec in iterator:
logger.info('Processing file {0}'.format(filename)
try:
record = source.Record(filename, raw_rec)
except Exception as e:
logger.warn("record parsing error: %s" % e)
continue
stks = mapper.find(record)
if not stks:
continue
regs = defaultdict(set)
for stk in stks:
regnm = exchanges[stk[2]]
regs[regnm].add(stk)
for reg,secs in regs.iteritems():
info = regions[reg]
outf = info.get_file(record.get_tm())
source.output(outf, record, secs, info.tz, conf)
logger.info('END')
日志刚刚停止,因为正在处理某些文件...
数据库连接随时可能断开,您的代码不处理这个问题。此外,计算机确实需要不时重启,例如断电、安全更新。
显然,还有其他原因可能导致您的代码中断,这就是为什么您需要将其运行 作为一项服务以防万一您想让它变得可靠。
了解如何 运行 此 python 脚本作为系统服务并将此服务配置为在失败时自动重启。这将阻止它停止,即使存在导致它崩溃的错误。如果它确实崩溃,Systemd 将重新启动它。
有很多资源对此进行了记录,但您可以从 https://learn.adafruit.com/running-programs-automatically-on-your-tiny-computer/systemd-writing-and-enabling-a-service
开始