Setting a PHP script as a Windows Service

Maybe the Resource Kit Tools (specifically srvany.exe) can help you here. MSDN: How To Create A User-Defined Service and possibly this hint for 2008 Server should help you setup any executable as a service. (I’ve successfully used this on Windows 2003 Server, Windows 2008 Server and on Windows XP Professional [other Resource Kit, though])

You’d create a bat containing php your-script.php, wrap that with srvany.exe and voila, the script is started once the machine loads the services.

srvany.exe should handle those start/stop/restart calls you’d expect a daemon to execute. It would load your executable on start, kill the process on stop, do both on restart. So you don’t have to worry about this part. You might want to check if a register_shutdown_function() can help identify when your service process is killed.

You can even define dependencies to other services (say some database or some such).

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\THENAMEOFYOURSERVICE]
"DependOnService"="DEPENDONTHIS"

replace THENAMEOFYOURSERVICE with the name you gave your service and DEPENDONTHIS with the name of the service to depend on (say “Postgres9.0” or something). Save that file to dependency.reg and load it with regedit /s dependency.reg. (Or doubleclick it in explorer…)

Leave a Comment