随笔 - 612, 文章 - 0, 评论 - 31, 阅读 - 74万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

PHP win32service - Windows 系统后台服务

Posted on   eastson  阅读(110)  评论(0编辑  收藏  举报

https://www.php.net/manual/zh/book.win32service.php

<?php
//No timeouts, Flush Content immediatly
set_time_limit(0);
ob_implicit_flush();

//Service Settings
$phpPath = "D:\\PHPSTUDY\\Extensions\\php\\php7.3.4nts";
$ServiceName = 'phpServiceName';
$ServiceDisplay = 'phpDisplayName';

//Windows Service Control
$ServiceAction = "status";

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 (is_int($ServiceStatus)) {
        echo "Error Code: 0x" . dechex($ServiceStatus) . "\nSee https://www.php.net/manual/en/win32service.constants.errors.php\n\n";
        exit;
    }
    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
    $errorCode = win32_create_service( Array(
        'service' => $ServiceName,
        'display' => $ServiceDisplay,
        'params' => __FILE__ . " run",
        'path' => $phpPath . "\\php.exe",
    ));
    if ($errorCode) {
        echo "Error Code: 0x" . dechex($errorCode) . "\nSee https://www.php.net/manual/en/win32service.constants.errors.php\n\n";
    } else {
        echo "Service Installed\n\n";
    }
    exit;
} else if ( $ServiceAction == "uninstall" ) {
    //Remove Windows Service
    $errorCode = win32_delete_service($ServiceName);
    if ($errorCode) {
        echo "Error Code: 0x" . dechex($errorCode) . "\nSee https://www.php.net/manual/en/win32service.constants.errors.php\n\n";
    } else {
        echo "Service Removed\n\n";
    }
    exit;
} else if( $ServiceAction == "start") {
    //Start Windows Service
    $errorCode = win32_start_service($ServiceName);
    if ($errorCode) {
        echo "Error Code: 0x" . dechex($errorCode) . "\nSee https://www.php.net/manual/en/win32service.constants.errors.php\n\n";
    } else {
        echo "Service Started\n\n";
    }
    exit;
} else if( $ServiceAction == "stop" ) {
    //Stop Windows Service
    $errorCode = win32_stop_service($ServiceName);
    if ($errorCode) {
        echo "Error Code: 0x" . dechex($errorCode) . "\nSee https://www.php.net/manual/en/win32service.constants.errors.php\n\n";
    } else {
        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);

    //YOUR CODE HERE
    echo "YOUR CODE HERE\n";
}

//Exit
if ( $ServiceAction == "run" ) {
    win32_set_service_status(WIN32_SERVICE_STOPPED);
}

exit();

注意权限问题,要以管理员身份执行php脚本。
可能碰到的错误码:https://www.php.net/manual/en/win32service.constants.errors.php

相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2014-05-10 How to authenticate a user by uid and password?
2014-05-10 LDAP 中 CN,OU,DC 的含意
2014-05-10 Apache Directory Studio
2014-05-10 LDAP编辑器 LDAPAdmin
2013-05-10 Windows XP下获取OpenERP源码
2013-05-10 关于OpenERP客户端字体问题的一点心得
点击右上角即可分享
微信分享提示