为什么在 PHP CLI 中 getmypid() 返回 0 作为 pid?

发布于 2024-09-11 23:56:16 字数 263 浏览 5 评论 0原文

我正在开发一个长时间运行的命令行脚本,当然我希望不要多次启动它自己。因此,我编写了一个 .pid 文件,并在启动时检查是否存在具有此类 PID 的进程。 问题是在 Windows 上它返回 0 作为 PID,所以我总是通过该 PID 运行进程(系统空闲有 PID 0)。

有人知道如何解决这个问题吗?我可以制作一个在 Windows 上运行的守护进程吗?也许修复将通过 pcntl_fork 进行分叉?

PS 我使用 Yii 和它的 CConsoleCommand 类作为基础。

I'm developing a long running command line script and of course I want in not to launch itself more than once. So I write a .pid file and on launch check if a process with such PID exists.
The problem is on Windows it returns 0 as a PID, so I always get a process by that PID running (System Idle has PID 0).

Does someone know how to fix that? Can I make a daemon running on windows? Maybe the fix will be forking with pcntl_fork?

P.S. I'm using Yii and it's CConsoleCommand class as a base.

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

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

发布评论

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

评论(1

我纯我任性 2024-09-18 23:56:16

我可能是错的,但我不认为如果你写出 .pid,在 Windows 上它不会检查该文件。

您可以检查该文件是否存在,如果存在则不要再次运行。而且 afaik pcntl_fork 不能在 Windows 上运行(默认情况下,使用某种 *nix 模拟器它可能会工作)

I could be wrong but I don't think if you write out a .pid, that on windows it'll check for the file.

You could check if the file exists and if it does then don't run again. And afaik pcntl_fork doesn't run on windows (defaultly that is, with some sort of *nix emulator it might work)

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