Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a PHP script as a Windows Service

I need to set up a PHP script as a windows service.

I need it to run regardless of which user is logged in, and on system start up - so it sounds like a windows service is best, but am happy to hear other suggestions.

(This script runs continuously, it's not a "run every 5 mins" thing I could use the Scheduled Task Manager for.)

http://support.microsoft.com/kb/251192 covers using the sc.exe program to install your service.

But from what I've read, I need to have a wrapper round the PHP script to accept the special commands from the windows service manager. Can anyone help with this?

like image 644
James Avatar asked May 10 '11 15:05

James


People also ask

Can PHP run on Windows server?

Installation on Windows systems ¶ Installing PHP on modern Microsoft Windows systems and recommended configuration with common web servers. The Official releases of PHP on Windows are recommended for production use. However, you are welcome to build PHP from Source. You will need a Visual Studio environment.

How do I run a PHP script in Powershell?

Open powershell and type c:\php\php.exe -h , you will get the php help output. Yay you are up and running, whoot. Type env into os search (cortana) and select environmental variables. Now you can run php in powershell with php ( php -h to test).


3 Answers

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…)

like image 191
rodneyrehm Avatar answered Sep 22 '22 05:09

rodneyrehm


We used FireDaemon for this task, it doesn't require wrapper scripts etc. Unfortunately it's not freeware.

like image 33
abaumg Avatar answered Sep 26 '22 05:09

abaumg


You can run php on command line by giving different parameters and also the script-file as parameter. If you add the whole line you need into the service configuration it should run. So you are also able to try the device before creating the service. If the php-script is outside your web-route perhaps you should add the folder to the PATH-Variable of windows.

like image 33
David Avatar answered Sep 24 '22 05:09

David