linux implements php to perform cron task detail regularly

  • 2020-11-26 18:44:20
  • OfStack

There is no set of solutions for PHP itself to perform timed tasks, but it is done with the aid of sleep functions. In this way, 1 configuration should be done in advance, such as the implementation process:


ignore_user_abort();// Close your browser, PHP The script can also continue to execute .
    set_time_limit(0);//  through set_time_limit(0) You can let the program run indefinitely 
    $interval=60*30;//  Run every half hour 
    do{
        // Here is the code you want to execute    
        sleep($interval);//  Waiting for the 5 minutes 
    }while(true);

However, I have some performance concerns about this approach, but it is a temporary approach.

The method Recommended by me is implemented in scripts, using the timing task mechanism of OS itself and windows using bat script. I haven't tried window, though. So Let me talk about the implementation in linux.

If your web server is based on linux, use cron job under linux. In the case of RedHat5, we only need the logical code to be executed periodically in advance. For example demo php


<?php
echo "Hello";
?>

Then right php shell script 1 encapsulation, shell script invokes the demo. php, demo. sh code is as follows:


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

Once the shell script is written, make sure it has enough permissions for this line, for example :/bin/chmod u+x ES48en.sh.

Then configure cronjob on linux, which is installed by default on linux. If your task needs to be performed by the hour, day, week, or month, you can copy your ES56en.sh script to

etc/ hourly, etc/cron, daily, etc/cron, monthly, monthly, ok, you can finish your task. If at some point you want to remove the timer task, either remove move from folder above or simply delete ok.

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

Please refer to the special configuration about cron: http: / / www pantz. org software/cron/croninfo html

Here I run the script every 2 minutes between 12 a.m. and 14 p.m., so configure it as follows (for example, demo.sh is in /tmp) :

First, execute ES99en-ES100en from the command line of linux, and then type the rule into it:


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

After typing, press "Esc" on the keyboard, and then type "wq" to exit the editing page. You can then use ES108en-ES109en to view the cron you just edited.

By this time the special cron has been completed. For example, you are just now with the demo linux account to complete the steps above, there is one simple way is directly can edit/var spool cron/demo this file, can be directly

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

Using OS to manage your timer tasks is quick and you don't have to worry about performance unless you have a script problem. This method is easy to maintain, can modify scheduled execution schedule, and can easily remove and add other scheduled tasks.


Related articles: