©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
(PECL win32service SVN)
win32_create_service — Creates a new service entry in the SCM database
$details
[, string $machine
] )
details
An array of service details:
service
The short name of the service. This is the name that you will use to control the service using the net command. The service must be unique (no two services can share the same name), and, ideally, should avoid having spaces in the name.
display
The display name of the service. This is the name that you will see in the Services Applet.
description
The long description of the service. This is the description that you will see in the Services Applet.
user
The name of the user account under which you want the service to run. If omitted, the service will run as the LocalSystem account. If the username is specified, you must also provide a password.
password
The password that corresponds to the user
.
path
The full path to the executable module that will be launched when the service is started. If omitted, the path to the current PHP process will be used.
params
Command line parameters to pass to the service when it starts. If you want to run a PHP script as the service, then the first parameter should be the full path to the PHP script that you intend to run. If the script name or path contains spaces, then wrap the full path to the PHP script with ".
load_order
Controls the load_order. This is not yet fully supported.
svc_type
Sets the service type. If omitted, the default value is
WIN32_SERVICE_WIN32_OWN_PROCESS
.
Don't change this unless you know what you're doing.
start_type
Specifies how the service should be started. The default is
WIN32_SERVICE_AUTO_START
which means the
service will be launched when the machine starts up.
error_control
Informs the SCM what it should do when it detects a problem with
the service. The default is
WIN32_SERVER_ERROR_IGNORE
. Changing this
value is not yet fully supported.
delayed_start
If delayed_start
is set to TRUE
, then this
will inform the SCM that this service should be started after other
auto-start services are started plus a short delay.
Any service can be marked as a delayed auto-start service; however,
this setting has no effect unless the service's
start_type
is
WIN32_SERVICE_AUTO_START
.
This setting is only applicable on Windows Vista and Windows Server 2008 or greater.
base_priority
To reduce the impact on processor utilisation, it may be necessary to set a base priority lower than normal.
The base_priority
can be set to one of the
constants define in
Win32 Base Priority Classes.
machine
The optional machine name on which you want to create a service. If omitted, it will use the local machine.
成功时返回 WIN32_NO_ERROR
,参数错误时返回 FALSE
,或失败时返回一个 Win32 错误码。
Example #1 A win32_create_service() example
Create a service with the short name 'dummyphp'.
<?php
$x = win32_create_service (array(
'service' => 'dummyphp' , # the name of your service
'display' => 'sample dummy PHP service' , # short description
'description' => 'This is a dummy Windows service created using PHP.' , # long description
'params' => '"' . __FILE__ . '" run' , # path to the script and parameters
));
debug_zval_dump ( $x );
?>
[#1] Anonymous [2010-10-08 10:05:55]
[An example of how to create a Windows service. Evaluate code first and use at your own risk!]
<?php
//No timeouts, Flush Content immediatly
set_time_limit(0);
ob_implicit_flush();
//Service Settings
$phpPath = "D:\\php\\php5.2.9";
$ServiceName = 'phpServiceName';
$ServiceDisplay = 'phpDisplayName';
//Windows Service Control
$ServiceAction = "status";
//$ServiceAction = "debug";
if ( isset($_GET['ServiceAction']) and strlen($_GET['ServiceAction']) ) {
$ServiceAction = addslashes($_GET['ServiceAction']);
} else if ( isset($argv) and isset($argv[1]) and strlen($argv[1]) ) {
$ServiceAction = $argv[1];
}
if( $ServiceAction == "status" ) {
$ServiceStatus = win32_query_service_status($ServiceName);
if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_STOPPED ) {
echo "Service Stopped\n\n";
} else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_START_PENDING ) {
echo "Service Start Pending\n\n";
} else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_STOP_PENDING ) {
echo "Service Stop Pending\n\n";
} else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_RUNNING ) {
echo "Service Running\n\n";
} else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_CONTINUE_PENDING ) {
echo "Service Continue Pending\n\n";
} else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_PAUSE_PENDING ) {
echo "Service Pause Pending\n\n";
} else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_PAUSED ) {
echo "Service Paused\n\n";
} else{
echo "Service Unknown\n\n";
}
exit;
} else if ( $ServiceAction == "install" ) {
//Install Windows Service
win32_create_service( Array(
'service' => $ServiceName,
'display' => $ServiceDisplay,
'params' => __FILE__ . " run",
'path' => $phpPath."\\php.exe",
));
echo "Service Installed\n\n";
exit;
} else if ( $ServiceAction == "uninstall" ) {
//Remove Windows Service
win32_delete_service($ServiceName);
echo "Service Removed\n\n";
exit;
} else if( $ServiceAction == "start") {
//Start Windows Service
win32_start_service($ServiceName);
echo "Service Started\n\n";
exit;
} else if( $ServiceAction == "stop" ) {
//Stop Windows Service
win32_stop_service($ServiceName);
echo "Service Stopped\n\n";
exit;
} else if ( $ServiceAction == "run" ) {
//Run Windows Service
win32_start_service_ctrl_dispatcher($ServiceName);
win32_set_service_status(WIN32_SERVICE_RUNNING);
} else if ( $ServiceAction == "debug" ) {
//Debug Windows Service
set_time_limit(10);
} else {
exit();
}
//Server Loop
while (1) {
//Handle Windows Service Request
usleep(100*1000);
if ( $ServiceAction == "run" ) {
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);
}
}
//User Loop
sleep(1);
echo "\n<BR>YOUR CODE HERE";
}
//Exit
if ( $ServiceAction == "run" ) {
win32_set_service_status(WIN32_SERVICE_STOPPED);
}
exit();
?>
[#2] pauljamesthomson at gmail dot com [2007-01-22 04:54:29]
Other start_type values:
0x00000002: A service started automatically by the service control manager during system startup. For more information, see Automatically Starting Services.
0x00000000: A device driver started by the system loader. This value is valid only for driver services.
0x00000003: A service started by the service control manager when a process calls the win32_start_service() function.
0x00000004: A service that cannot be started. Attempts to start the service result in the error code
0x00000001: A device driver started by the IoInitSystem function. This value is valid only for driver services.
I can confirm that 0x00000003 works as expected (service is created, but must be started manually).
More here:
http://msdn2.microsoft.com/en-us/library/ms682450.aspx