Linux implementation PHP timed cron task Detailed _php example

Source: Internet
Author: User
Tags php script sleep function

There is no solution for PHP itself to perform timed tasks, but with the sleep function. This side is to do some early configuration, such as the implementation process:

Copy Code code as follows:

Ignore_user_abort ()//Turn off the browser, the PHP script can also continue to execute.
Set_time_limit (0);//Through Set_time_limit (0) allows the program to execute indefinitely
$interval =60*30;//run every half hour
do{
Here's 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 the script to implement, using the OS itself Timing task mechanism, Windows play with bat script. But I didn't try it on the window. So I'm going to talk about implementation in Linux.

If your Web server is based on Linux, you can do it with a cron job under Linux. Take REDHAT5 as an example, we need only the logical code to be executed in advance. such as demo.php

Copy Code code as follows:

<?php
echo "Hello";
?>

And then, yes? PHP uses a shell script package that invokes the demo.php,demo.sh code in the shell script as follows:

Copy Code code as follows:

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

Once you have finished writing your shell script, make sure that it has enough of this line of permissions, such as:/bin/chmod u+x demo.sh.

Then configure the Linux cronjob,cronjob to be installed by default on Linux. If your task is to be performed by hours, days, weeks, and months, you can directly copy your demo.sh script to

/etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly on the OK, so you can complete your task. If you want to remove a timed task at a certain point in time, move from the folder above to the other or delete it right away.

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

For 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 between 12 o'clock in the morning to 14 o'clock every day, and then configure it 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:

Copy Code code as follows:

*/2 12-14 * * * */tmp/demo.sh

Press the "ESC" key on the keyboard after the entry is complete, and then enter: Wq, and the edit page exits. Then you can use Crontab-l to view the cron job you just edited.

At this point, the special cron is done. For example, you just use Linux demo account to complete the above steps, then there is a simple way to directly edit/var/spool/cron/demo this file, you can directly

Modify your cron job. For example: Vi/var/spool/cron/demo

Using the OS to manage your timing tasks is fast and doesn't worry about performance unless you have some problems with your script. This way is easy to maintain, can modify scheduled execution of the plan, you can easily remove and add additional timing 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.