downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

win32_create_service> <win32_ps_stat_proc
[edit] Last updated: Mon, 01 Nov 2010

view this page in

CLXVIII. win32service 関数

導入

win32service は Windows 限定の拡張モジュールです。サービスコントロールマネージャと 連携してサービスの開始・停止・登録・解除を行ったり、PHP スクリプトを サービスとして実行させる機能を提供します。

要件

Windows NT、Windows 2000、Windows XP あるいは Windows Server 2003 が必要です。Windows NT 系の Windows に対応しています。

インストール手順

PECL からのインストール

  1. http://snaps.php.net/win32/ から php_win32service.dll がダウンロード可能です。 PHP のバージョンに一致する PECL_X_X フォルダを選択します。

  2. php_win32service.dll を extension_dir にコピーします。

  3. php.ini で拡張モジュールを読み込みます。

    extension=php_win32service.dll

例 1. PHP スクリプトをサービスとして登録する

<?php
win32_create_service
(array(
       
'service' => 'dummyphp',                 # サービスの名前
       
'display' => 'sample dummy PHP service', # 説明
       
'params' => 'c:\path\to\script.php run', # スクリプトへのパスとパラメータ
   
));
?>

例 2. サービスの登録を解除する

<?php
win32_delete_service
('dummyphp');
?>

例 3. サービスとして実行する

<?php
if ($argv[1] == 'run') {
 
win32_start_service_ctrl_dispatcher('dummyphp');

  while (
WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
   
# ここに実際の作業内容を書きます。
    # 1 回のループに 30 秒以上かからないように心がけてください。
 
}
}
?>

定義済み定数

以下の定数が定義されています。 この関数の拡張モジュールが PHP 組み込みでコンパイルされているか、 実行時に動的にロードされている場合のみ使用可能です。

WIN32_SERVICE_CONTROL_CONTINUE (integer)

WIN32_SERVICE_CONTROL_INTERROGATE (integer)

WIN32_SERVICE_CONTROL_PAUSE (integer)

WIN32_SERVICE_CONTROL_STOP (integer)

WIN32_SERVICE_CONTROL_HARDWAREPROFILECHANGE (integer)

WIN32_SERVICE_CONTROL_POWEREVENT (integer)

WIN32_SERVICE_CONTROL_SESSIONCHANGE (integer)

WIN32_ERROR_CALL_NOT_IMPLEMENTED (integer)

WIN32_NO_ERROR (integer)

WIN32_SERVICE_RUNNING (integer)

WIN32_SERVICE_STOPPED (integer)

WIN32_SERVICE_STOP_PENDING (integer)

WIN32_SERVICE_WIN32_OWN_PROCESS (integer)

WIN32_SERVICE_INTERACTIVE_PROCESS (integer)

WIN32_SERVICE_STOPPED (integer)

WIN32_SERVICE_START_PENDING (integer)

WIN32_SERVICE_STOP_PENDING (integer)

WIN32_SERVICE_RUNNING (integer)

WIN32_SERVICE_CONTINUE_PENDING (integer)

WIN32_SERVICE_PAUSE_PENDING (integer)

WIN32_SERVICE_PAUSED (integer)

WIN32_SERVICE_ACCEPT_NETBINDCHANGE (integer)

WIN32_SERVICE_ACCEPT_PARAMCHANGE (integer)

WIN32_SERVICE_ACCEPT_PAUSE_CONTINUE (integer)

WIN32_SERVICE_ACCEPT_SHUTDOWN (integer)

WIN32_SERVICE_ACCEPT_STOP (integer)

WIN32_SERVICE_ACCEPT_HARDWAREPROFILECHANGE (integer)

WIN32_SERVICE_ACCEPT_POWEREVENT (integer)

WIN32_SERVICE_ACCEPT_SESSIONCHANGE (integer)

WIN32_SERVICE_FILE_SYSTEM_DRIVER (integer)

WIN32_SERVICE_KERNEL_DRIVER (integer)

WIN32_SERVICE_WIN32_SHARE_PROCESS (integer)

WIN32_SERVICE_RUNS_IN_SYSTEM_PROCESS (integer)

目次
win32_create_service -- SCM データベースに新しいサービスのエントリを作成する
win32_delete_service -- SCM データベースからサービスのエントリを削除する
win32_get_last_control_message -- サービスに送信された直近の制御メッセージを返す
win32_query_service_status -- サービスの状態を問い合わせる
win32_set_service_status -- サービスの状態を更新する
win32_start_service_ctrl_dispatcher --  スクリプトを SCM に登録し、指定した名前でサービスとして稼動させる ようにする
win32_start_service -- サービスを開始する
win32_stop_service -- サービスを停止する


add a note add a note User Contributed Notes win32service 関数
brian dot ngure at gmail dot com 30-Apr-2010 03:03
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]);

?>
me at sylvain tiret besse point fr 20-Nov-2007 12:45
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 !

 
show source | credits | sitemap | contact | advertising | mirror sites