(PECL win32service SVN)
win32_start_service_ctrl_dispatcher — Registers the script with the SCM, so that it can act as the service with the given name
When launched via the Service Control Manager, a service process is required to "check-in" with it to establish service monitoring and communication facilities. This function performs the check-in by spawning a thread to handle the lower-level communication with the service control manager.
Once started, the service process should do 2 things. The first is to tell the Service Control Manager that the service is running. This is achieved by calling win32_set_service_status() with the WIN32_SERVICE_RUNNING constant. If you need to perform some lengthy process before the service is actually running, then you can use the WIN32_SERVICE_START_PENDING constant. The second is to continue to check-in with the service control manager so that it can determine if it should terminate. This is achieved by periodically calling win32_get_last_control_message() and handling the return code appropriately.
Returns TRUE on success, FALSE if there is a problem with the parameters or a Win32 Error Code on failure.
Beispiel #1 A win32_start_service_ctrl_dispatcher() example
Check if the service is runnig under the SCM.
<?php
if (!win32_start_service_ctrl_dispatcher('dummyphp')) {
die("I'm probably not running under the service control manager");
}
win32_set_service_status(WIN32_SERVICE_START_PENDING);
// Some lengthy process to get this service up and running.
win32_set_service_status(WIN32_SERVICE_RUNNING);
while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
# do some work here, trying not to take more than around 30 seconds
# before coming back into the loop again
}
?>
Insert value of params between " if the string have a space:
<?php
// First you need to create a service, you only need to do this once
/*
win32_create_service(array(
'service' => 'myphpservice',
'display' => 'My PHP Service',
'params' => '"c:\\my folder\myphpservice.php"',
'path' => 'c:\\PHP\\php.exe'));
*/
?>
<?php
// First you need to create a service, you only need to do this once
/*
win32_create_service(array(
'service' => 'myphpservice',
'display' => 'My PHP Service',
'params' => 'c:\\myphpservice.php',
'path' => 'c:\\PHP\\php.exe'));
*/
$myservicename = 'myphpservice';
// Connect to service dispatcher and notify that startup was successful
if (!win32_start_service_ctrl_dispatcher($myservicename)) die('Could not connect to service :'.$myservicename);
win32_set_service_status(WIN32_SERVICE_RUNNING);
// Main Server Loop
while (1) {
switch (win32_get_last_control_message()) {
case WIN32_SERVICE_CONTROL_CONTINUE: break; // Continue server routine
case WIN32_SERVICE_CONTROL_INTERROGATE: win32_set_service_status(WIN32_NO_ERROR); break; // Respond with status
case WIN32_SERVICE_CONTROL_STOP: win32_set_service_status(WIN32_SERVICE_STOPPED); exit; // Terminate script
default: win32_set_service_status(WIN32_ERROR_CALL_NOT_IMPLEMENTED); // Add more cases to handle other service calls
}
// Main script goes here
sleep(10); // Run every 10 seconds
}
win32_set_service_status(WIN32_SERVICE_STOPPED);
?>
Don't try to call "win32_start_service_ctrl_dispatcher" too late in your code. You'd trigger a #2186 error : "The service is not responding to the control function." (from commande line) or a #1053 error : "The service did not respond to the start or control request in a timely fashion." (from services GUI).
Try not to load a bunch of PEAR classes before to register php script as Service like I did.
Another good hint, to get a verbose version of the return code, you can call "NET HELPMSG ###" from command line where ### is your error code.