PHP timed Task/cron Job

Source: Internet
Author: User
Tags sleep function

For PHP itself there is no set of solutions to perform timed tasks, but it is done with the sleep function. Such a party is to do some configuration in advance, such as the implementation process:

Ignore_user_abort();//turn off the browser and the PHP script will continue to execute.    Set_time_limit(0);//set_time_limit (0) allows the program to be implemented without restrictions    $interval=60*30;//running every half hour     Do{        //Here is the code you want to execute        Sleep($interval);//wait 5 minutes} while(true);

But I have some concerns about performance in this way, but it's also a temporary approach.

I recommend the way to use scripts to implement, using the OS itself, the timing of the task mechanism, Windows on the use of bat script. But I did not try on the window. So I'll talk about the implementation in Linux.

If your Web server is Linux-based, you can do it with a cron job under Linux. Taking REDHAT5 as an example, we only need to pre-execute logic code in advance. such as demo.php

<? PHP Echo "Hello";? >

And then, yes. PHP uses a shell script as a wrapper, calling the demo.php,demo.sh code in the shell script as follows:

#! /bin/bash#if you php install to/usr/local/php//usr/local/php/bin/php/home/xx-user/demo.php

After you have finished writing the shell script, make sure that it has sufficient permissions for this line, for example:/bin/chmod u+x demo.sh.

Then configure cronjob,cronjob on Linux to be installed by default on Linux. If your task is to be executed by the hour, day, week, and month, then you can copy your demo.sh script directly to

/etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly are OK, so you can complete your task. If you want to remove timed tasks at a point in time, move from the folder above to the other location or delete them directly.

If your script has a special execution time, such as Tuesday per week, or 15th number per month. Then you need to configure your own cron job.

For a special configuration of Cron, please refer to: http://www.pantz.org/software/cron/croninfo.html

I'm going to run this script every 2 minutes from 12 o'clock in the morning to 14 o'clock in the morning, and the configuration is as follows (for example, Demo.sh is located in the/tmp directory):

First, execute the CRONTAB-E on the Linux command line, and then enter the rules into it:

*/2- * * * *  /tmp/demo.  SH

With the input completed, press the "ESC" key on the keyboard, then enter: Wq, edit the page and exit. Then you can use Crontab-l to view the cron job you just edited.

The special cron will be finished by this time. For example, you just used Linux demo account to complete the above steps, then there is a simple way is directly can edit/var/spool/cron/demo this file, you can directly

Modify your cron job. Example: Vi/var/spool/cron/demo

Using the OS to manage your scheduled tasks is fast, and you don't have to worry about performance issues unless your script itself has some problems. This approach is easy to maintain, can modify scheduled execution schedules, and can easily remove and add additional scheduled tasks.

PHP timed Task/cron Job

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.