Home php教程 php手册 PHP自动定时循环执行任务

PHP自动定时循环执行任务

Jun 06, 2016 pm 07:35 PM
php use timing cycle perform tasks test automatic

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

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

代码写的比较垃圾,勿喷!

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);

?>

Copy after login

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;  //开启定时任务

?>

Copy after login
PHP自动定时循环执行任务
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles