views:

27

answers:

1

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.

A: 

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)

Viper_Sb
I check for the PID myself, I write a PID file and on next run check if file exists and if it does - read the PID and launch tasklist.exe to check if that PID is running. The bad thing is getmypid returns 0, not the process pid.
Psihius
Which version of windows and PHP? I'm on Windows 7 and PHP 5.3.2, and it works fine for me. I run "print getmypid(); flush(); sleep(35);" then in another cmd window I run tasklist.exe and it shows up in there. Have you tried to manually see if it shows in tasklist? If it does perhaps your parsing is incorrect?
Viper_Sb
Actually I found out that I made a type in the function name, I was calling getmygid() and not getmypid(). Now it works just fine. Sorry for the bothering :)
Psihius