PHP Scheduled Tasks

Source: Internet
Author: User
(1) The program starts once after the scheduled task is set up, and the program runs until the server restarts.
(2) If you run it repeatedly, it may not be the result you want, so you will start multiple same scheduled tasks
(3) If each page include this page, then start a number of permanently running programs consume unnecessary system resources,
It also affects the normal access of the page, if placed in front of the page will always be waiting for the return status (that is a dead loop)
<?php
Ignore_user_abort (TRUE); PHP scripts can continue to execute even if the client disconnects (such as turning off the browser).
Set_time_limit (0); Execution time is unlimited, php default execution time is 30 seconds, through Set_time_limit (0) can allow the program to execute indefinitely
$interval =60*5; Run every 5 minutes
$f = ' lock.txt ';
if (file_exists ($f)) {//To determine if a tag file exists, exits to prevent duplicate operation
Exit ();
}
do{
if (@get_file_contents ($f) = = ' Stop ') {//Set stop condition, stop when only write to Lock.txt stop
Break
}
@file_put_contents ($f, ' run ');//write a file repeatedly, flag already running scheduled task
.... Omit task code
Sleep ($interval);//program paused for 5 minutes
}while (TRUE);
@unlock ($f); Delete a tag file
?>

This is just one way of implementing code, and one way to do this is to use the PHP CLI pattern to implement the scheduled task.

Use Run->cmd as window

Run the Php.exe, then use the at, according to the requirements of the input can be

Different systems have different methods, and Linux is different. It didn't work out, but it's definitely OK.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.