PHP定時任務 - PHP自動定時循環執行任務實例代碼

PHP定時任務已使用測試,現在已經趨於穩定,效果良好。本功能需要配合監控寶,360監控等使用。創建監控WEB項目,指定到任務主文件URL!如果沒有Memcache可以創建text文件寫入time替代!不過執行時沒有memcache準確!可更改config.php文件內的任務開啓關閉變量,對任務進行開啓,關閉操作!

1.task.php(任務主文件)

<?php
 
ignore_user_abort(); //關掉瀏覽器,PHP腳本也可以繼續執行.
set_time_limit(0); // 通過set_time_limit(0)可以讓程序無限制的執行下去
$interval = 15; // 每隔*秒運行
$temp_key = 0;
do {
    $time = time();
    require 'config.php';
    $mem = new Memcache();
    $mem->connect($Memcache_server, $Memcache_port);
    if ($is_send) {
        $get_time = $mem->get('tem_data');
    } else {
        $get_time = $time + 86400;
        exit();
    }
    if ($get_time == $time) {
        $mem->close();
        exit();
    } else if ($get_time > $time - $interval) {
        $mem->close();
    } else if ($temp_key == 0) {
        $temp_key = 1;
        @file_get_contents('http://******/*****.php');
        $mem->set('tem_data', $time, MEMCACHE_COMPRESSED, $Memcache_date);
        $mem->close();
        sleep(mt_rand(1, 3));
        $temp_key = 0;
    }else{
        $mem->close();
        exit();
    }
    //這裏是你要執行的代碼
    sleep($interval); // 等待*秒鐘
} while (true);
?>

2.config.php(配置文件)

<?php
 
$Memcache_server = '127.0.0.1';
$Memcache_port = 11211;
$Memcache_date = 86400;
//return $is_send = false; //關閉定時任務
return $is_send = true;  //開啓定時任務
?>

3.執行效果記錄

PHP定時任務 - PHP自動定時循環執行任務實例代碼
PHP定時任務 - PHP自動定時循環執行任務實例代碼

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

參考自:www.91huoke.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章