为什么在 PHP CLI 中 getmypid() 返回 0 作为 pid?
我正在开发一个长时间运行的命令行脚本,当然我希望不要多次启动它自己。因此,我编写了一个 .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.
我可能是错的,但我不认为如果你写出 .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)