打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

代码写的比较垃圾,勿喷!
标签: <无>

代码片段(3) [全屏查看所有代码]

1. [代码]task.php(任务主文件)     跳至 [1] [2] [全屏预览]

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
32
33
34
35
36
37
<?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(配置文件)     跳至 [1] [2] [全屏预览]

1
2
3
4
5
6
7
8
<?php
$Memcache_server = '127.0.0.1';
$Memcache_port = 11211;
$Memcache_date = 86400;
//return $is_send = false; //关闭定时任务
return $is_send = true;  //开启定时任务
?>

3. [图片] QQ图片20140909104720.jpg    

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
开启微擎性能优化redis 微擎memcache
PHP开发APP接口(三):核心技术
phpize是什么?
编程语言安装memcache及php扩展
win7下memCache的安装过程(具体操作步骤)
Linux下的Memcache安装
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服