Setting a PHP script as a Windows Service

后端 未结 7 1218
春和景丽
春和景丽 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: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
    

提交回复
热议问题