如何检查 Linux 进程以确定进程如何/何时死亡/终止?

发布于 2024-09-30 07:06:29 字数 187 浏览 4 评论 0原文

我有一个 python irc bot,我通过执行 /etc/init.d/ 以 root 身份启动它芬妮开始。但有时它会死亡,而且似乎是在一夜之间发生的。

我可以做什么来检查它并在文本文件中查看进程的状态?

I have a python irc bot which I start up as root by doing /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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

伴梦长久 2024-10-07 07:06:29

如果您知道它仍在运行,您可以 pstack 它以查看它的回传。我不确定这有多大用处,因为您将看到解释器的调用堆栈。您也可以像其他人提到的那样尝试 strace 或 ltrace 。

我还要确保在脚本运行的任何环境中,您都设置了 ulimit -c unlimited ,以便在 python 完全崩溃的情况下生成核心。

我可能尝试的另一件事是让这项工作由不等待其子项的父项执行。这应该会导致 proc 表条目作为僵尸保留下来,即使底层作业已经退出。

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.

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.

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.

呆头 2024-10-07 07:06:29

如果您对真正低级别的进程活动感兴趣,您可以在 strace 下运行 python 解释器标准错误重定向到文件。

如果您只想在机器人崩溃时检查 python 代码,并且您在源代码中找到了发生崩溃的位置,则可以使用 try/except 和 except 子句中进入调试器

import pdb; pdb.set_trace()

不过,您可能需要在非守护程序模式下运行您的机器人才能正常工作。

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.

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 theexcept clause:

import pdb; pdb.set_trace()

You'll probably need to run your bot in non-daemon mode for that to work, though.

随风而去 2024-10-07 07:06:29

您可能想尝试 Python psutils,这是我已经使用过并且有效的东西。

You might want to try Python psutils, it is something that I have used and works.

嗼ふ静 2024-10-07 07:06:29

获得有关该问题的一些额外线索的一种廉价方法是启动 phenny

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

,当它崩溃时,检查 /tmp/phenny.out 的尾部以获取 Python 回溯。

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.

夜光 2024-10-07 07:06:29

如果您只需要验证进程是否正在运行,您只需运行一个脚本来检查命令

ps ax | 的输出。 grep [p]henny

每隔几秒。如果它是空的,那么显然该进程已经死了。

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 | grep [p]henny

every few seconds. If it's empty, then obviously the process is dead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文