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 = 'MyServiceFinal';
$Display = 'My PHP Service';
$host = '127.0.0.1'; $port = 23; $max = 20; if (!isset($argv[1])){ShowHelp(); exit;}
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;
} else if ($argv[1] == 'uninstall') {
$x = win32_delete_service($Service);
debug_zval_dump($x);
echo "Service Removed\n\n";
exit;
} elseif($argv[1] == "start") {
$x = win32_start_service($Service);
debug_zval_dump($x);
echo "Service Started\n\n";
exit;
} elseif($argv[1] == "stop") {
$x = win32_stop_service($Service);
debug_zval_dump($x);
echo "Service Stopped\n\n";
exit;
} else if ($argv[1] != 'run') {
ShowHelp(); exit();
}
if (!win32_start_service_ctrl_dispatcher($Service)) die("Could not connect to service : $Service");
win32_set_service_status(WIN32_SERVICE_RUNNING);
set_time_limit(0);
ob_implicit_flush();
while (1) {
usleep(500);
switch (win32_get_last_control_message()) {
case WIN32_SERVICE_CONTROL_CONTINUE: break; case WIN32_SERVICE_CONTROL_INTERROGATE: win32_set_service_status(WIN32_NO_ERROR); break; case WIN32_SERVICE_CONTROL_STOP: win32_set_service_status(WIN32_SERVICE_STOPPED); exit; default: win32_set_service_status(WIN32_ERROR_CALL_NOT_IMPLEMENTED); }
}
win32_set_service_status(WIN32_SERVICE_STOPPED);
exit;
function ShowHelp(){
echo "Usage:
install:\t installs servce
uninstall:\t deletes service
start:\t\t Start the windows service
stop:\t\t Stops the windows service
run:\t\t called by CMS to run the service
For information on code numbers type 'net helpmsg xxxx'
EXAMPEL: 'net helpmsg 1072'
";
}
?>