简体   繁体   English

如何检查Linux进程以确定进程终止/终止的方式/时间?

[英]How can I inspect a linux process to determine how/when a process dies/terminates?

I have a python irc bot which I start up as root by doing /etc/init.d/phenny start . 我有一个python irc bot ,我通过/etc/init.d/phenny start以root身份/etc/init.d/phenny start Sometimes it dies, though and it seems to happen overnight. 有时它会死掉,但似乎一夜之间就会发生。

What can I do to inspect it and see the status of the process in a text file? 我该怎么做才能检查它并在文本文件中查看进程的状态?

If you're interested in really low level process activity, you can run the python interpreter under strace with standard error redirected to a file. 如果您对真正的低级流程活动感兴趣,则可以在strace下运行python解释器,并将标准错误重定向到文件。

If you're only interested in inspecting the python code when your bot crashes, and you have the location in the source where the crash happens, you can wrap that location with try / except and break into the debugger in the except clause: 如果您只对机器人崩溃时检查python代码感兴趣,并且您在发生崩溃的源中有位置,则可以使用try / except包装该位置并在except子句中使用break进入调试器

import pdb; pdb.set_trace()

You'll probably need to run your bot in non-daemon mode for that to work, though. 但是,您可能需要以非守护进程模式运行机器人才能使用它。

If you know it's still running, you can pstack it to see it's walkback. 如果您知道它仍在运行,则可以对其进行堆叠以查看其后退。 I'm not sure how useful that will be because you will see the call stack of the interpreter. 我不确定它会有多大用处,因为你会看到解释器的调用堆栈。 You could also try strace or ltrace as someone else mentioned. 您也可以像其他人提到的那样尝试strace或ltrace。

I would also make sure that in whatever environment the script runs in, you have set ulimit -c unlimited so that a core is generated in case python it is outright crashing. 我还要确保在脚本运行的任何环境中,你已经设置了ulimit -c unlimited,以便在python完全崩溃的情况下生成核心。

Another thing I might try is to have this job executed by a parent that does not wait it's child. 我可能尝试的另一件事是让不等待孩子的父母执行此作业。 This should cause the proc table entry to stick around as a zombie even when the underlying job has exited. 即使底层作业已退出,这也应该导致proc表项作为僵尸保留。

你可能想尝试Python psutils ,这是我使用和工作的东西。

A cheap way to get some extra clues about the problem would be to start phenny with 一个廉价的方法来获得关于这个问题的一些额外线索将是开始与

/etc/init.d/phenny start 2>/tmp/phenny.out 1>&2

When it crashes, check the tail of /tmp/phenny.out for the Python traceback. 当它崩溃时,检查/tmp/phenny.out的尾部是否有Python追溯。

If you only need to verify that the process is running you could just run a script that checks the output of command 如果您只需要验证进程是否正在运行,则可以运行一个检查命令输出的脚本

ps ax | ps ax | grep [p]henny grep [p] henny

every few seconds. 每隔几秒钟。 If it's empty, then obviously the process is dead. 如果它是空的,那么显然这个过程已经死了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM