os.kill 没有引发 OSError,但是我没有看到给定的 pid 正在运行

发布于 2024-08-12 14:17:38 字数 410 浏览 2 评论 0原文

在我的 ubuntu 服务器上,我运行以下命令:

python -c 'import os; os.kill(5555, 0)'

这样做是为了查看 pid 5555 是否正在运行。根据我的理解,如果 pid 没有运行,这应该会引发 OSError 。这不会对我引发 OSError,这意味着它应该是一个正在运行的进程。但是,当我运行时:

ps aux | grep 5555

我看到没有使用该 pid 运行的进程。这种情况也会发生在该一般范围内的其他几个 pid 上,但不会发生在 555 或 55555 上。

有谁知道为什么 os.kill 不会像预期的那样引发 OSError 吗?

注意:这是在 python 2.5.1 下运行的。

On my ubuntu server I run the following command:

python -c 'import os; os.kill(5555, 0)'

This is done so that I can see if pid 5555 is running. From my understanding this should raise an OSError if the pid is not running. This is not raising an OSError for me which means it should be a running process. However when I run:

ps aux | grep 5555

I see no process running with that pid. This also happens on several other pids in that general range, but it does not happen with say 555 or 55555.

Does anyone have any insight as to why os.kill would not raise an OSError like it is expected to?

Note: this is running under python 2.5.1.

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

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

发布评论

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

评论(4

将军与妓 2024-08-19 14:17:38

在linux下,每个进程和每个线程都有不同的pid。 os.kill 并不关心您是否有线程 pid,或者任务 pid,但是 ps 通常不会显示线程 pid。

例如,在我的机器上,PID 为 8502 的进程正在运行线程,您可以像这样看到

$ ls /proc/8502/task/
8502  8503  8504  8505  8506  8507  8511  8512  8514  8659

注意,8503 没有出现在进程列表中

$ ps aux | grep [8]503
$

,但是使用更多 ps 参数您可以看到它

$ ps -eLf | grep [8]503
ncw       8502     1  8503  0   10 10:00 ?        00:00:00 /usr/lib/virtualbox/VBoxSVC --automate

(Grepping for [8]503 意味着 grep 不会出现 - 这是一个古老的 UNIX 技巧!)

现在让我们看看它是否还活着

$ python
Python 2.6.4 (r264:75706, Nov  2 2009, 14:44:17)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Loaded customisations from '/home/ncw/.pystartup'
>>> import os
>>> os.kill(8503, 0)
>>>

这会重复您的问题。

我想如果你这样做

ls /proc/*/task/5555

或者

ps -eLf | grep [5]555

你会看到罪魁祸首。

Under linux, each process and each thread have a different pid. os.kill doesn't care whether you have a thread pid, or a task pid, however ps doesn't normally show the thread pids.

For instance on my machine the process with PID 8502 is running threads which you can see like this

$ ls /proc/8502/task/
8502  8503  8504  8505  8506  8507  8511  8512  8514  8659

Note that 8503 doesn't appear in the process list

$ ps aux | grep [8]503
$

However using some more ps arguments you can see it

$ ps -eLf | grep [8]503
ncw       8502     1  8503  0   10 10:00 ?        00:00:00 /usr/lib/virtualbox/VBoxSVC --automate

(Grepping for [8]503 means that the grep won't show up - it's an old unix trick!)

Now lets see if it is alive or not

$ python
Python 2.6.4 (r264:75706, Nov  2 2009, 14:44:17)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Loaded customisations from '/home/ncw/.pystartup'
>>> import os
>>> os.kill(8503, 0)
>>>

This duplicates your problem.

I think if you do

ls /proc/*/task/5555

or

ps -eLf | grep [5]555

You will see the culprit thread.

梦里兽 2024-08-19 14:17:38

尝试安装 htop (sudo apt-get install htop),它有时会显示 ps 不显示的进程。

Try installing htop (sudo apt-get install htop), it sometimes displays process that ps doesn't.

时光沙漏 2024-08-19 14:17:38

也许这是2.5的一个错误?在 2.6.4 上我得到:

gruszczy@gruszczy-laptop:~$ python -c 'import os; os.kill(5555, 0)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OSError: [Errno 3] No such process

我相信,有一个错误报告:

http://mail.python.org/pipermail/new-bugs-announce/2009-February/004222.html

Maybe it's a bug in 2.5? On 2.6.4 I get:

gruszczy@gruszczy-laptop:~$ python -c 'import os; os.kill(5555, 0)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OSError: [Errno 3] No such process

I believe, there is a bug report for this:

http://mail.python.org/pipermail/new-bugs-announce/2009-February/004222.html

青瓷清茶倾城歌 2024-08-19 14:17:38

我不知道为什么在某些情况下不会引发 OSError,但值得注意的是,在基于 linux 和 unix 的操作系统上有一个最大 pid 值:

gt; cat /proc/sys/kernel/pid_max
32768

I don't know why that OSError is not raised in some cases, but it's important to note that there is a max pid value on linux and unix based OS:

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