Detailed description of cron task execution in php in linux

Source: Internet
Author: User
Tags sleep function
Linux's implementation of php's scheduled cron task does not have a solution for PHP itself to execute the scheduled task, 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:

The code is as follows:
Ignore_user_abort (); // close the browser and run the PHP script.
Set_time_limit (0); // use set_time_limit (0) to allow unlimited execution of the program
$ Interval = 60*30; // run every 30 minutes
Do {
// Here is the code you want to execute
Sleep ($ interval); // wait for 5 minutes
} While (true );

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

The code is as follows:
Echo "Hello ";

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

The code is as follows:
#! /Bin/bash
# If 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:

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:

The code is as follows:
*/2 12-14 ***/tmp/demo. sh

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: 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.