Jump to content

Running PHP file as a services


Recommended Posts

Well, it started when I've developed a socket server using PHP. Currently I use the command line below to start the server:

php -q Server.php

 

So I want it to be installed on the server as a service. So I can do something like this:

service MyPHPServer start [Linux]
sc start MyPHPServer [windows]

 

So far I've got this which is using cmd to launch a batch file and the batch file will start the server:

C:\Windows\System32\cmd.exe /c C:\my_service_path\startup.bat (I've tried only in Windows)

 

Then I put this line in my batch file (assumed the php file is located same directory as the batch file):

php -q Server.php

 

It was added to the service registry. But when I tried to start it, i got a message:

[sC] StartService FAILED 1053:

The service did not respond to the start or control request in a timely fashion.

 

I know this is not the way, just wonder anyone tried to start a PHP file as a service before and share their experience.  ;)

Link to comment
Share on other sites

Hi...

 

To have a program run as a Windows service, it has to have handling for certain interrupts and signals (has to implement a certain set of code).

 

 

Basically it's impossible with a PHP script without heavily modifying the PHP core.

Link to comment
Share on other sites

To be honest, I'm not sure what signals I'm suggesting be implemented either.  I'm quite curious on this now, so I might try to make a PHP-script as a service later.  I didn't know that script-level PHP could accept signals.  I guess I should have expected that :).  (To think, I doubted PHP!)

 

Now, I'm not sure as to what the signals exactly are, since in C++ using managed code in Visual Studio, the code of a service extends System::ServiceProcess::ServiceBase and implements OnStart and OnStop (there's also OnPause I think, but that's optional).

 

So it could actually be based on exports or something, but I would say 99.99999% chance it's based on signals, and that somewhere in the ServiceBase code it handles all that (except of course, that code isn't accessible, just the headers ;p).

 

 

 

If I get anything working later, I'll post it.

Link to comment
Share on other sites

Well you would have to know what signals Windows sends the program.  I more I think about it though, the more I'm not entirely sure if it is 100% signal based.  Haven't gotten around to trying it myself yet though.

 

 

Ahh, just googled, and it looks like Windows does not have signals in the sense that Unix does.... http://stackoverflow.com/questions/140111/sending-an-arbitrary-signal-in-windows

 

I think what I'll do is look into the Apache source to see how it handles services as I don't even know how to get started with service stuff without using managed code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.