Linux implementation of PHP timed cron task detailed

Source: Internet
Author: User
Tags php script sleep sleep function

 linux PHP to implement a cron task on a timed basis

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 of the process:     Code as follows: Ignore_user_abort ()//Turn off the browser, PHP script can continue to execute.     Set_time_limit (0);//Through Set_time_limit (0) can let the program unrestricted execution down     $interval =60*30;//run every half hour   & nbsp do{       //This is the code you want to execute.            sleep ($interval);/wait 5 minutes   &NB Sp }while (TRUE);     But I have some concerns about performance in this way, but it's also a temporary approach.   I recommend the way to implement the script, 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. For example demo.php   code is as follows: <?php echo "Hello";?>     then yes. PHP uses a shell script package that invokes the demo.php,demo.sh code in the shell script as follows: & nbsp   Code as follows: #! /bin/bash #if php Install to/usr/local/php//usr/local/php/bin/php/home/xx-user/demo.php     Complete shell script writing After that, make sure that it has enough of this line of permissions, for example:/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, then you can direct your demo.sh feetThis copy to  /etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly is 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.   Special configuration for 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 and 14 o'clock every day, then configure it as follows (for example, Demo.sh is located in the/tmp directory):   First executes the CRONTAB-E on the Linux command line, and then enters the rule into it:   Code as follows: */2 12-14 * * * *  /tmp/demo.sh     With input complete, press the "ESC" key on the keyboard, and then enter: Wq, the edit page exits. Then you can use Crontab-l to view the cron job you just edited.   To 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.