An example of how it should be done:
<?php
class TestClass {
private $args;
function __construct($arg) {
$this->args = $arg;
$this->run();
}
private function run() {
if ($this->args == 'run') {
win32_start_service_ctrl_dispatcher('test_service');
while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
# do your work here.
# try not to take up more than 30 seconds before going around the loop
# again
}
}
}
}
$object = new TestClass($argv[1]);
?>
CLXVIII. Fonctions win32service
Introduction
L'extension win32service est une extension spécifique à Windows qui autorise PHP à communiquer avec la Gestion de Contrôle de Service pour démarrer, arrêter, enregistrer ou effacer des services, et autorise aussi vos scripts PHP à s'exécuter en tant que service.
Pré-requis
Windows NT, Windows 2000, Windows XP ou Windows Server 2003. N'importe quelle version de windows dérivé de Windows NT devrait être compatible
Installation
Vous pouvez télécharger php_win32service.dll à partir de http://snaps.php.net/win32/. Choisissez le dossier PECL_X_X qui concorde avec votre version de PHP.
Copiez le fichier php_win32service.dll dans votre extension_dir.
Charge l'extension à partir de votre php.ini
extension=php_win32service.dll
Exemples
Constantes pré-définies
Ces constantes sont définies par cette extension, et ne sont disponibles que si cette extension a été compilée avec PHP, ou bien chargée au moment de l'exécution.
- WIN32_SERVICE_CONTROL_CONTINUE (entier)
- WIN32_SERVICE_CONTROL_INTERROGATE (entier)
- WIN32_SERVICE_CONTROL_PAUSE (entier)
- WIN32_SERVICE_CONTROL_STOP (entier)
- WIN32_SERVICE_CONTROL_HARDWAREPROFILECHANGE (entier)
- WIN32_SERVICE_CONTROL_POWEREVENT (entier)
- WIN32_SERVICE_CONTROL_SESSIONCHANGE (entier)
- WIN32_ERROR_CALL_NOT_IMPLEMENTED (entier)
- WIN32_NO_ERROR (entier)
- WIN32_SERVICE_RUNNING (entier)
- WIN32_SERVICE_STOPPED (entier)
- WIN32_SERVICE_STOP_PENDING (entier)
- WIN32_SERVICE_WIN32_OWN_PROCESS (entier)
- WIN32_SERVICE_INTERACTIVE_PROCESS (entier)
- WIN32_SERVICE_STOPPED (entier)
- WIN32_SERVICE_START_PENDING (entier)
- WIN32_SERVICE_STOP_PENDING (entier)
- WIN32_SERVICE_RUNNING (entier)
- WIN32_SERVICE_CONTINUE_PENDING (entier)
- WIN32_SERVICE_PAUSE_PENDING (entier)
- WIN32_SERVICE_PAUSED (entier)
- WIN32_SERVICE_ACCEPT_NETBINDCHANGE (entier)
- WIN32_SERVICE_ACCEPT_PARAMCHANGE (entier)
- WIN32_SERVICE_ACCEPT_PAUSE_CONTINUE (entier)
- WIN32_SERVICE_ACCEPT_SHUTDOWN (entier)
- WIN32_SERVICE_ACCEPT_STOP (entier)
- WIN32_SERVICE_ACCEPT_HARDWAREPROFILECHANGE (entier)
- WIN32_SERVICE_ACCEPT_POWEREVENT (entier)
- WIN32_SERVICE_ACCEPT_SESSIONCHANGE (entier)
- WIN32_SERVICE_FILE_SYSTEM_DRIVER (entier)
- WIN32_SERVICE_KERNEL_DRIVER (entier)
- WIN32;E_WIN32_SHARE_PROCESS (entier)
- WIN32;E_RUNS_IN_SYSTEM_PROCESS (entier)
- Table des matières
- win32_create_service -- Crée une nouvelle entrée pour service dans la base de données SCM
- win32_delete_service -- Supprime une entrée de service de la base de données SCM
- win32_get_last_control_message -- Retourne le dernier message de contrôle qui a été envoyé à ce service
- win32_query_service_status -- Questionne le statut d'un service
- win32_set_service_status -- Met à jour le statut d'un service
- win32_start_service_ctrl_dispatcher -- Enregistre un script avec SCM, alors il peut être interprété en tant que service avec le nom donné
- win32_start_service -- Démarre un service
- win32_stop_service -- Arrête un service
Just to help a little, the service control command must be in the first script called. If you try to type this portion of code in a separate file and include it, the SCM will not get the controls.
example :
- file1.inc :
<?PHP
class MyClass{
function __construct() {
$x = win32_start_service_ctrl_dispatcher('service');
while (WIN32_SERVICE_CONTROL_STOP!=win32_get_last_control_message()){
//your code
}
}
?>
- service.php :
<?PHP
include("file1.inc");
$object = new MyClass();
?>
WON'T WORK !
