How to create cron job using PHP?

前端 未结 12 1495
一整个雨季
一整个雨季 2020-11-21 05:50

I\'m new to using cron job. I don\'t even know how to write it. I have tried to search from internet, but I still don\'t understand it well. I want to create a cron job that

相关标签:
12条回答
  • 2020-11-21 06:06

    $command = "php ".CRON_PATH.php ";
    if(substr(php_uname(), 0, 7) == "Windows"){
    pclose(popen("start /B ". $command, "r"));
    }else{
    shell_exec($command ." > /dev/null &");
    }

    0 讨论(0)
  • 2020-11-21 06:08

    This is the best explanation with code in PHP I have found so far:

    http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php--net-19428

    In short:

    Although the syntax of scheduling a new job may seem daunting at first glance, it's actually relatively simple to understand once you break it down. A cron job will always have five columns each of which represent a chronological 'operator' followed by the full path and command to execute:

    * * * * * home/path/to/command/the_command.sh

    Each of the chronological columns has a specific relevance to the schedule of the task. They are as follows:

    Minutes represents the minutes of a given hour, 0-59 respectively.
    Hours represents the hours of a given day, 0-23 respectively.
    Days represents the days of a given month, 1-31 respectively.
    Months represents the months of a given year, 1-12 respectively.
    Day of the Week represents the day of the week, Sunday through Saturday, numerically, as 0-6 respectively.
    

    So, for example, if one wanted to schedule a task for 12am on the first day of every month it would look something like this:

    0 0 1 * * home/path/to/command/the_command.sh

    If we wanted to schedule a task to run every Saturday at 8:30am we'd write it as follows:

    30 8 * * 6 home/path/to/command/the_command.sh

    There are also a number of operators which can be used to customize the schedule even further:

    Commas is used to create a comma separated list of values for any of the cron columns.
    Dashes is used to specify a range of values.
    Asterisksis used to specify 'all' or 'every' value
    

    Visit the link for the full article, it explains:

    1. What is the format of the cronjob if you want to enter/edit it manually.
    2. How to use PHP with SSH2 library to authenticate as the user, which crontab you are going to edit.
    3. Full PHP class with all necessary methods for authentication, editing and deleting crontab entries.
    0 讨论(0)
  • 2020-11-21 06:08

    There is a simple way to solve this: you can execute php file by cron every 1 minute, and inside php executable file make "if" statement to execute when time "now" like this

    <?/** suppose we have 1 hour and 1 minute inteval 01:01 */
    
    $interval_source = "01:01";
    $time_now = strtotime( "now" ) / 60;
    $interval = substr($interval_source,0,2) * 60 + substr($interval_source,3,2);
    
    
    if( $time_now % $interval == 0){
    /** do cronjob */
    }
    
    0 讨论(0)
  • 2020-11-21 06:15

    First open your SSH server with username and password and change to the default root user(User with all permissions) then follow the steps below,

    1. enter the command crontab -l now you will see the list of all cronjobs.
    2. enter crontab -e a file with all cron jobs will be opened.
    3. Edit the file with your cronjob schedule as min hr dayofmonth month dayofweek pathtocronjobfile and save the file.
    4. Now you will see a response crontab: installing new crontab now again check the list of cronjobs your cron job will be listed there.
    0 讨论(0)
  • 2020-11-21 06:17

    Added to Alister, you can edit the crontab usually (not always the case) by entering crontab -e in a ssh session on the server.

    The stars represent (* means every of this unit):

    [Minute] [Hour] [Day] [Month] [Day of week (0 =sunday to 6 =saturday)] [Command]
    

    You could read some more about this here.

    0 讨论(0)
  • 2020-11-21 06:20

    Create a cronjob like this to work on every minute

    *       *       *       *       *       /usr/bin/php path/to/cron.php &> /dev/null
    
    0 讨论(0)
提交回复
热议问题