Home > php教程 > php手册 > body text

PHP自动定时循环执行任务

WBOY
Release: 2016-06-06 19:35:11
Original
2096 people have browsed it

经过本人N月使用测试,现在已经趋于稳定,效果良好。本功能需要配合监控宝,360监控等使用。创建监控WEB项目,指定到任务主文件URL!如果没有Memcache可以创建text文件写入time替代!不过执行时没有memcache准确!可更改config.php文件内的任务开启关闭变量,

经过本人N月使用测试,现在已经趋于稳定,效果良好。本功能需要配合监控宝,360监控等使用。创建监控WEB项目,指定到任务主文件URL!如果没有Memcache可以创建text文件写入time替代!不过执行时没有memcache准确!可更改config.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);
?>
Copy after login
<?php

$Memcache_server = '127.0.0.1';
$Memcache_port = 11211;
$Memcache_date = 86400;
//return $is_send = false; //关闭定时任务
return $is_send = true;  //开启定时任务
?>
Copy after login
PHP自动定时循环执行任务
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!