在linux for python脚本中,有没有办法监视导致程序关闭的原因?

在linux for python脚本中,有没有办法监视导致程序关闭的原因?,python,linux,amazon-web-services,amazon-ec2,Python,Linux,Amazon Web Services,Amazon Ec2,我试图弄明白为什么有时我的python脚本随机地从linux主机接收SIGINT 调试此问题的通常方法是什么 我目前正在使用 nohup python my_script.py > /dev/null 2>&1& 从那时起,我开始转向 nohup python my_script.py > myprogram.out 2> myprogram.err& 是否有其他日志和通道可以检查linux主机为何向脚本发送终止命令 我正在使用aws ec2实

我试图弄明白为什么有时我的python脚本随机地从linux主机接收
SIGINT

调试此问题的通常方法是什么

我目前正在使用

nohup python my_script.py > /dev/null 2>&1&
从那时起,我开始转向

nohup python my_script.py > myprogram.out 2> myprogram.err&
是否有其他日志和通道可以检查linux主机为何向脚本发送终止命令

我正在使用aws ec2实例。

请尝试以下方法:

import datetime
try:
    [your_code]
except KeyboardInterrupt as e:
    # catch SIGINT
    print("{}: {}".format(datetime.datetime.now(), e), flush=True)
except:
    # catch unknown exception
    print("{}: other exception!".format(datetime.datetime.now()), flush=True)

嗯,这怎么告诉我是谁引起了终止信号呢?你可以通过log infot进行调试,但是我已经知道
SIGINT
导致脚本关闭,我只是不知道为什么
SIGINT
发送到脚本
SIGINT
可能是由OOM Killer、crontab程序或其他人发送的。在捕获
SIGINT
后,您可以将linux中运行的进程列表打印到日志中。这是不可能的。您应该做的是尽可能多地记录日志,然后进行调试