linux - 如何查看python脚本自动退出原因?
迷茫
迷茫 2017-04-18 10:06:12
0
4
1033

使用gunicorn+flask框架,同时通过apscheduler实现对一款爬虫的定时调用,但是在这其中遇到了一个问题,如下:
1,我利用shell终端,通过gunicorn -c直接运行,然后通过flask构造url调用爬虫,并在终端观察输出信息,可以完美调用爬虫。但是当我运行gunicorn -c之后,关闭终端,则爬虫不能完美运行,通过对爬虫调试,发现在运行一段时间后就被终止。
2,之后我在爬虫中使用os.system('nohup python spider.py &'),并且一直打开终端可以完美运行,但是在关闭终端的情况下,又无法执行到底。
所以请问大家怎么来看一下这个脚本运行中为何自动结束。或者说产生的原因是什么。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
小葫芦

虽然你将进程放在后台执行,但是并没有跟启动该进程的终端detach,因此,父进程shell终端关闭,子进程也将被关闭。

解决方案1,可以参考How to make a Python script run like a service or daemon in Linux,教你如何在linux下编程将进程做成daemon进程。示例程序比较长,我放在最后讲解吧。

解决方案2,有python库可以将自身进程daemon化,而且挺多的。比如,python-daemon。使用示例如下:

import daemon

def do_main_program():
    pass  # 你的程序

with daemon.DaemonContext():
    do_main_program()
    

补充解决方案1的处理:

# -*- coding: utf-8 -*-
import os
import sys
import datetime

def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)  # Exit first parent.
    except OSError, e:
        sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)
    # Decouple from parent environment.
    os.chdir("/")
    os.umask(0)
    os.setsid()
    # Do second fork.
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)  # Exit second parent.
    except OSError, e:
        sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)
    # Redirect standard file descriptors.
    si = file(stdin, 'r')
    so = file(stdout, 'a+')
    se = file(stderr, 'a+', 0)
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())
    
if __name__ == "__main__":
    daemonize()
    # 然后开始执行你的其他程序。
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!