How to run a CodeIgniter file through CRON?

后端 未结 6 1530
旧时难觅i
旧时难觅i 2021-01-31 00:16

I\'ve tried the following method in the past:



        
相关标签:
6条回答
  • 2021-01-31 00:34

    You might also want to check this out: Cron job bootstrapper

    This is a simple bootstrapper file that you can use to directly run your CodeIgniter controllers from the commandline. It’s a very easy and elegant solution for using CI controllers for cron jobs. It also supports logging.

    0 讨论(0)
  • 2021-01-31 00:40

    Use php-cli instead of php
    Ex:

    /usr/bin/php-cli  /home/CPANEL_USER/public_html/index.php cronJobs deleteNotifications 
    
    0 讨论(0)
  • 2021-01-31 00:45

    The simplest way to run a cron via CodeIgniter is to make a cron URL available via your app.

    Then call it via wget

    wget -O - -q -t 1 http://www.example.com/cron/run
    

    Inside the controller you can then use a log to ensure the cron is not run too often i.e. if the Google robots trigger it by mistake.

    A second method would be to use lynx

    /usr/local/bin/lynx -source http://www.example.com/cron/run
    
    0 讨论(0)
  • 2021-01-31 00:47

    If you want to run cron job by running url, here is a great article

    http://www.nbill.co.uk/documentation/setting-up-a-cronjob.html

    0 讨论(0)
  • 2021-01-31 00:52

    There is a wiki article about how to run CodeIgniter on the command line, but this is more useful for applications that need to interact with the user through terminal (there's a library for that too).

    http://codeigniter.com/wiki/CI_on_the_command_line/

    One benefit of doing it this way over using wget is you can protect your code from being run by users or bots with:

    if(!empty($_SERVER['HTTP_HOST']))
    {
         show_error('Shove off hax0r!');
    }
    
    0 讨论(0)
  • 2021-01-31 00:55

    You may also like to add --spider to ignore the response. This stops the request from timing out:

    wget -O - -q -t 1 --spider http://www.example.com/cron/run
    
    0 讨论(0)
提交回复
热议问题