Setting a PHP script as a Windows Service

后端 未结 7 1081
春和景丽
春和景丽 2020-12-01 03:29

I need to set up a PHP script as a windows service.

I need it to run regardless of which user is logged in, and on system start up - so it sounds like a windows ser

相关标签:
7条回答
  • 2020-12-01 03:37

    You can run php on command line by giving different parameters and also the script-file as parameter. If you add the whole line you need into the service configuration it should run. So you are also able to try the device before creating the service. If the php-script is outside your web-route perhaps you should add the folder to the PATH-Variable of windows.

    0 讨论(0)
  • 2020-12-01 03:48

    Maybe the Resource Kit Tools (specifically srvany.exe) can help you here. MSDN: How To Create A User-Defined Service and possibly this hint for 2008 Server should help you setup any executable as a service. (I've successfully used this on Windows 2003 Server, Windows 2008 Server and on Windows XP Professional [other Resource Kit, though])

    You'd create a bat containing php your-script.php, wrap that with srvany.exe and voila, the script is started once the machine loads the services.

    srvany.exe should handle those start/stop/restart calls you'd expect a daemon to execute. It would load your executable on start, kill the process on stop, do both on restart. So you don't have to worry about this part. You might want to check if a register_shutdown_function() can help identify when your service process is killed.

    You can even define dependencies to other services (say some database or some such).

    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\THENAMEOFYOURSERVICE]
    "DependOnService"="DEPENDONTHIS"
    

    replace THENAMEOFYOURSERVICE with the name you gave your service and DEPENDONTHIS with the name of the service to depend on (say "Postgres9.0" or something). Save that file to dependency.reg and load it with regedit /s dependency.reg. (Or doubleclick it in explorer…)

    0 讨论(0)
  • 2020-12-01 03:50

    Loop in shell.

    1. In php loop add loop counter and exit after hour for restarting process.
    2. memory usage controll
    3. reconect to db each 100 seconds

    Shell script do simple loop and each for iteration create new logfile

    PHP and shell script:

        ini_set('memory_limit', '300M');
        $loopCnt = 0;
        while(true) {
            /**
             * Maximal time limit for loop execution
             */
            set_time_limit(10);
    
            $loopCnt ++;
            /**
             * each hour finishing
             */
            if($loopCnt > 60 * 60){
                exit;
            }
            usleep(self::SLEEP_MICROSECONDS);
            if ($loopCnt % 60 === 0) { //log every 60 seconds memory usage
                $this->out('memory usage: '.memory_get_usage());
                //reconnect DB to avoid timeouts and server gone away errors
                Yii::$app->db->close();
                Yii::$app->db->open();
            }
            if (memory_get_usage() > self::MEMORY_LIMIT) {
                $this->out('memory limit reached: '.self::MEMORY_LIMIT . ' actual:  ' . memory_get_usage() . ' exit');
                exit;
            }
            
            /**
            *  do work
            */
    
        }
    
    }
    

    // bat file

     set loopcount=1000000
    
     :loop
    
         echo Loop %DATE% %TIME% %loopcount%
    
         set t=%TIME: =0%
    
         php cwbouncer.php > C:\logs\cwbouncer_%DATE:~2,2%%DATE:~5,2%%DATE:~8,2%_%t:~0,2%%t:~3,2%%t:~6,2%.log
    
        set /a loopcount=loopcount-1
    
        if %loopcount%==0 goto exitloop
    
        goto loop
    
    :exitloop
    
    0 讨论(0)
  • 2020-12-01 03:53

    After a few days... i found this magnific option!

    He build an .exe that recive the service options and works fine!

    https://superuser.com/questions/628176/php-cgi-exe-as-a-windows-service/643676#643676

    the command correct:

    sc create FOO binPath= "service.exe \"C:\php\php-cgi.exe -b 127.0.0.1:9000 -c C:\php\php.ini"\" type= own start= auto error= ignore DisplayName= "FOO php"

    0 讨论(0)
  • 2020-12-01 03:54

    I found this but haven't tried it myself. PHP actually comes with some functions to do this: http://uk.php.net/manual/en/book.win32service.php

    Here are some examples: http://uk.php.net/manual/en/win32service.examples.php

    <?php
    if ($argv[1] == 'run') {
      win32_start_service_ctrl_dispatcher('dummyphp');
    
      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
      }
    }
    ?>
    
    0 讨论(0)
  • 2020-12-01 03:59

    We used FireDaemon for this task, it doesn't require wrapper scripts etc. Unfortunately it's not freeware.

    0 讨论(0)
提交回复
热议问题