How can I write a PHP cron script?

后端 未结 3 807
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 09:04

I\'m sorry if this is a silly question, but in reality I have no clue.

I wrote this app to compress and move files from a client\'s PC to a master PC.
It works

相关标签:
3条回答
  • 2021-01-07 09:37

    There are no problems running a PHP script as a cron job. What you need to do is provide the full path in the filesystem to the script file, and use the command php to tell Linux what program to run the file with.

    Example:

    */5 * * * * php /var/www/vhosts/statistikk/cron/getLastArticleFromIntranet.cron.php >> /var/www/vhosts/statistikk/cron/LOG.txt 2> /dev/null
    

    This script will run every 5 minutes, all days of the week. Whatever the PHP-script echoes / prints out, will be stored to the LOG.txt file so that I can monitor the scripts events.

    Try running just the command in your shell before putting it in the cronjobs to make sure it works.

    However, you say that you normally call this script with a AJAX call. This will not be possible to do with a cronjob. I assume you use AJAX to pass along some $_POST-elements that the script needs. So what you have to do is either adapt the script to allow $argv parameters as well, and add them to the crontab job, or simply make a script which does not need any given parameters before it runs.

    If you are going to adapt your script to support $argv parameters, follow the answer already existing on Stack Overflow about adding parameters to the job:

    How to run a php url with parameters in cron tab

    EDIT:

    I'd just like to add to my answer as from the answer below. To edit you crontab jobs in Linux you can simply use the command crontab -e.

    This is the description of each required param that needs to be filled.

    *     *     *   *    *        command to be executed
    -     -     -   -    -
    |     |     |   |    |
    |     |     |   |    +----- day of week (0 - 6) (Sunday=0)
    |     |     |   +------- month (1 - 12)
    |     |     +--------- day of        month (1 - 31)
    |     +----------- hour (0 - 23)
    +------------- min (0 - 59)
    
    0 讨论(0)
  • 2021-01-07 09:38

    To add a cronjob in linux do this:

    # crontab -e if vi is your editor press i to enter text and esc to exit. Use :w to save your changes and :q to quit. If nano is your choosen editor, then use strg+o for saving and str+x for closing.

    Anyway add this line to the bodom of the file: */5 * * * * php runScript.php

    It will be called every 5min.

    * * * * * php /path/to/file/runScript.php
    ┬ ┬ ┬ ┬ ┬
    │ │ │ │ │
    │ │ │ │ └──── Day of the week (0-7, Sunday is 0 or 7)
    │ │ │ └────── Month (1-12)
    │ │ └──────── Day (1-31)
    │ └────────── Hour (0-23)
    └──────────── Minute (0-59) 
    

    That should be it :)

    0 讨论(0)
  • 2021-01-07 09:51

    A common method for running PHP scripts from a cron job is to use a command-line program such as curl or wget. For example, the cron job runs a command similar to the following command:

    curl http://*****.com/script.php
    

    n this command, curl retrieves the web page, which then runs the PHP script.

    However, there is a better way to run PHP scripts on your web site from cron jobs. You can run the script directly by using the PHP command-line interpreter. This method is just as effective, and usually faster. The following command shows how to run a script using the PHP command-line interpreter:

    /usr/local/bin/php -q /home/username/public_html/script.php
    

    In this example, the PHP command-line interpreter runs the script.php file. The -q option enables quiet mode, which prevents HTTP headers from being displayed.

    Depending on the code in your PHP script, it may only run correctly when called from a specific directory. For example, if the script uses relative paths to include files, it will only run if it is called from the correct directory. The following command shows how to call a PHP script from a specific directory:

    cd /home/username/public_html/; /usr/local/bin/php -q script.php
    

    If your script requires special configuration options, you can use a custom php.ini file. The -c option allows you to call a PHP script using a custom php.ini file:

    /usr/local/bin/php -c /home/username/php.ini /home/username/public_html/script.php
    

    Click Here to More About Cron PHP Script Define with Example.

    0 讨论(0)
提交回复
热议问题