PHP scheduled task execution/Cron Job

Source: Internet
Author: User
Tags sleep function

There is no solution for PHP itself to execute scheduled tasks, but it is completed by using the sleep function. In this way, you need to make some configuration in advance, such as the implementation process:

();    (0);    =60*30;            ();    }();

However, I have some concerns about the performance of this method, but it is also a temporary method.

I recommend that you use scripts to implement it. By using the scheduled task mechanism of the OS itself, windows will use bat scripts. However, I have not tried it on the window. Let's talk about the implementation in linux.

If your web server is based on linux, you can use cron job in linux. Taking RedHat5 as an example, we only need to regularly execute the logic code in advance. For example, demo. php

<? "Hello"?>

Then, php is encapsulated with shell script 1, and demo. php is called in shell script. The demo. sh code is as follows:

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

After writing the shell script, make sure that it has sufficient permissions, such as/bin/chmod u + x demo. sh.

Then configure cronjob on linux. cronjob is installed by default on linux. If your task is executed by hour, day, week, month, you can directly copy your demo. sh script

/Etc/cron. hourly,/etc/cron. daily,/etc/cron. weekly,/etc/cron. monthly, And you can complete your task. If you want to remove a scheduled task at a certain time point, move it from the folder above to another place or delete it directly.

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

For specific cron configurations, see: http://www.pantz.org/software/cron/croninfo.html

Here I run this script every two minutes from twelve o'clock A.M. to every day. The configuration is as follows (for example, demo. sh is in the/tmp directory ):

First, execute crontab-e in the linux Command Line, and then input the rule into it:

*/ - * * *  /tmp/demo.

After the input is complete, press the "Esc" key on the keyboard and enter: wq. The editing page will exit. Then you can use crontab-l to view the cron job you just edited.

At this time, the special cron will be completed. For example, if you just used the demo account in linux to complete the above steps, you can directly edit the/var/spool/cron/demo file.

Modify your cron job. For 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 unless your script has some problems. This method is easy to maintain and allows you to modify scheduled execution plans, and easily remove and add other scheduled tasks.

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.