©
このドキュメントでは、 php中国語ネットマニュアル リリース
(PECL win32service SVN)
win32_set_service_status — Update the service status
$status
[, int $checkpoint
= 0
] )Informs the SCM of the current status of a running service. This call is only valid for a running service process.
status
The service status code, one of
WIN32_SERVICE_RUNNING
,
WIN32_SERVICE_STOPPED
,
WIN32_SERVICE_STOP_PENDING
,
WIN32_SERVICE_START_PENDING
,
WIN32_SERVICE_CONTINUE_PENDING
,
WIN32_SERVICE_PAUSE_PENDING
,
WIN32_SERVICE_PAUSED
.
checkpoint
The checkpoint value the service increments periodically to report its progress during a lengthy start, stop, pause, or continue operation. For example, the service should increment this value as it completes each step of its initialization when it is starting up.
The checkpoint
is only valid when the
status
is one of
WIN32_SERVICE_STOP_PENDING
,
WIN32_SERVICE_START_PENDING
,
WIN32_SERVICE_CONTINUE_PENDING
or
WIN32_SERVICE_PAUSE_PENDING
.
成功时返回 TRUE
,参数错误时返回 FALSE
,或失败时返回一个 Win32 错误码。
[#1] paul_nelson57071 at yahoo dot com [2010-05-13 11:04:20]
So after much searching I finally found all the peices to make a working windows service & server including:
- Installing & Uninstalling the service
- Starting & Stopping the Service
- Actually running a basic Server
- Responding to windows Service to Stop/Start/Restart
I hope you find this useful! I did this using PHP 5.2 and the PECL library for 5.2 (PHP 5.3 at the time does not have a PECL Library compiled for it)
<?php
//Service Settings
$Service = 'MyServiceFinal';
$Display = 'My PHP Service';
//Server Settings
$host = '127.0.0.1'; //Bind to IP #
$port = 23; //On port #
$max = 20; //Maximum Clients
//Exit if no start-up argument is passed
if (!isset($argv[1])){ShowHelp(); exit;}
//Create Windows Service
if ($argv[1] == 'install') {
$x = win32_create_service(array(
'service' => $Service,
'display' => $Service,
'params' => __file__ . ' run',
'path' => 'c:\\php\\php.exe',
));
debug_zval_dump($x);
echo "Service Installed\n\n";
exit;
//Remove Windows Service
} else if ($argv[1] == 'uninstall') {
$x = win32_delete_service($Service);
debug_zval_dump($x);
echo "Service Removed\n\n";
exit;
//Start Windows Service
} elseif($argv[1] == "start") {
$x = win32_start_service($Service);
debug_zval_dump($x);
echo "Service Started\n\n";
exit;
//Stop Windows Service
} elseif($argv[1] == "stop") {
$x = win32_stop_service($Service);
debug_zval_dump($x);
echo "Service Stopped\n\n";
exit;
//Unkown Command
} else if ($argv[1] != 'run') {
ShowHelp(); exit();
}
//Tell windows the service has started...
if (!win32_start_service_ctrl_dispatcher($Service)) die("Could not connect to service : $Service");
win32_set_service_status(WIN32_SERVICE_RUNNING);
// No timeouts, flush content immediatly
set_time_limit(0);
ob_implicit_flush();
//Server Loop
while (1) {
usleep(500);
//Handle Windows Service Request
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
}
//Acutal Server Code
}
//Clean up and Exit
win32_set_service_status(WIN32_SERVICE_STOPPED);
exit;
/
function Service_Main()
{
// YOU APPLICATION CODE HERE !!!
sleep(1); // dummy something
}
$SERVICE_NAME = "phptestservice";
$SERVICE_DISPLAY = "Test Service with PHP";
// so u can get: $SERVICE_PATH_PARTS["dirname"] $SERVICE_PATH_PARTS["basename"] $SERVICE_PATH_PARTS["extension"]
$SERVICE_PATH_PARTS = pathinfo(__FILE__);
$SERVICE_PARAMS = " run";
if (!isset($argv[1]))
{
die("this application need to be installed as a service.\n run with param install");
}
if ($argv[1] == 'install')
{
$x = win32_create_service(array(
'service' => $SERVICE_NAME,
'display' => $SERVICE_DISPLAY,
'params' => __FILE__ . $SERVICE_PARAMS,
//'path' => $SERVICE_PATH_PARTS["dirname"] . '\php.exe'
));
debug_zval_dump($x);
exit;
}
else if ($argv[1] == 'uninstall')
{
$x = win32_delete_service('dummyphp');
debug_zval_dump($x);
exit;
}
else if ($argv[1] != 'run')
{
die("bogus args, needs to run as service");
}
// Connect to service dispatcher and notify that startup was successful
if (!win32_start_service_ctrl_dispatcher($SERVICE_NAME)) die('Could not connect to service :'.$SERVICE_NAME);
win32_set_service_status(WIN32_SERVICE_RUNNING);
// Main Server Loop
while (1)
{
NTServiceResponder();
// Main script goes here
Service_Main();
sleep(1); // at least 1 sec delay per loop
}
win32_set_service_status(WIN32_SERVICE_STOPPED);
function NTServiceResponder()
{
switch (win32_get_last_control_message())
{
case 0: // PATCH for: seems never to go to 4 (WIN32_SERVICE_CONTROL_INTERROGATE)
win32_set_service_status(WIN32_SERVICE_RUNNING);
return TRUE;
break;
case WIN32_SERVICE_CONTROL_CONTINUE:
return TRUE; // "Continue"
case WIN32_SERVICE_CONTROL_INTERROGATE:
win32_set_service_status(WIN32_SERVICE_RUNNING);
return TRUE; // 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
}
return FALSE;
}
?>