杀死python脚本并查看最后一行执行

Pep*_*zza 1 python debugging process stack-trace

我有一个python脚本工作在无限循环,脚本做了几件事,并使用类中的几个方法,将信息记录到我拥有的几个记录器,并与SQS亚马逊队列一起使用.像这样的东西:

A = ClassA()
B = ClassB()
C = ClassC()

while True:
    # pull messages from SQS
    messages = sqs.pull_messages()
    logger.info('Pulled messages from SQS')
    A.do_something(messages)
    logger.info('Doing something on class A')
    # download something from the internet
    data = B.download_something()
    logger.info('Downloaded something')
    C.insert_to_database()
Run Code Online (Sandbox Code Playgroud)

该脚本可以正常工作几天而不会耗尽内存或退出脚本因为回溯,我在我的Linux框中启动了这样的脚本:

python script.py &
Run Code Online (Sandbox Code Playgroud)

几天之后,我可以回到盒子里,发现脚本仍在运行,但记录器记录的数据直到1天前,这不是常见的模式,有时脚本会在数小时后停止工作,有时会在几天后停止工作,但永远不会被杀死.

所以我的问题是,有没有一种方法我称之为杀死过程kill pid,看看发生了什么?启动验尸调试器会告诉我脚本正在做什么,或者在我杀死它之前它在哪一行?有没有办法接受这个?

小智 5

strace将为您提供系统调用及其响应的视图.它可能比您喜欢的级别低,但它可以让您查看网络呼叫是否失败,读取超时或可能发生的任何事情.

在标准的亚马逊linux图像strace可用'yum install strace'

找一个程序"mycommand"

# find the pid
ps -ef | grep [m]ycommand
# use the pid from the previous command
strace -p $thepid
Run Code Online (Sandbox Code Playgroud)