Linux crontab timed task invoke CI framework PHP code

Source: Internet
Author: User

Purpose: A website may need to perform several scheduled tasks, such as cleaning up the cache every hour, generating a sitemap Once a day, and backing up the database once a week ... If all is written in crontab, it may not be easy to manage and user configuration, use this class to easily add scheduled tasks, view the log of the execution Plan task, and manage the execution time of each scheduled task in the background. Installation: 1 database Add Table:
  1. CREATE TABLE ' Cron_schedule ' (
  2. ' schedule_id ' int (ten) unsigned not NULL auto_increment,
  3. ' Job_code ' varchar (255) Not NULL default ' 0 ',
  4. ' Status ' enum (' Pending ', ' running ', ' success ', ' missed ', ' error ') not NULL default ' pending ',
  5. ' Messages ' text,
  6. ' Created_at ' datetime not NULL default ' 0000-00-00 00:00:00 ',
  7. ' Scheduled_at ' datetime not NULL default ' 0000-00-00 00:00:00 ',
  8. ' Executed_at ' datetime not NULL default ' 0000-00-00 00:00:00 ',
  9. ' Finished_at ' datetime not NULL default ' 0000-00-00 00:00:00 ',
  10. PRIMARY KEY (' schedule_id '),
  11. KEY ' Task_name ' (' Job_code '),
  12. KEY ' Scheduled_at ' (' scheduled_at ', ' status ')
  13. ) Engine=myisam DEFAULT Charset=utf8;
2/application/config/config.php Add:
  1. /*
  2. |--------------------------------------------------------------------------
  3. | Cron Schedule Variables
  4. |--------------------------------------------------------------------------
  5. |
  6. | ' Enable_cron ' = whether to turn on cron schedule
  7. | ' Cron_schedule_table_name ' = table that holds cron schedule
  8. | ' Cron_schedule_generate_every ' = time interval to generate cron schedule
  9. | ' cron_schedule_ahead_for ' = cron Schedule each time it is produced
  10. | ' Cron_schedule_lifetime ' = the expiration date of cron schedule
  11. | ' Cron_history_cleanup_every ' = time interval to clear cron schedule
  12. | ' Cron_history_success_lifetime ' = save time for running a successful cron schedule
  13. | ' Cron_history_failure_lifetime ' = save time for cron schedule that failed to run
  14. |
  15. */
  16. $config [' enable_cron '] = TRUE;
  17. $config [' cron_schedule_table_name '] = ' cron_schedule ';
  18. $config [' cron_schedule_generate_every '] = 15;
  19. $config [' cron_schedule_ahead_for '] = 20;
  20. $config [' cron_schedule_lifetime '] = 15;
  21. $config [' cron_history_cleanup_every '] = 10;
  22. $config [' cron_history_success_lifetime '] = 60;
  23. $config [' cron_history_failure_lifetime '] = 600;
3/application/libraries/cron_schedule.php See annex 4 new/application/config/cron_schedules.php add Scheduled tasks to this file, such as:
  1. <?php
  2. $cron _schedule[' clear_log ') = Array (
  3. ' Schedule ' = Array (
  4. ' Config_path ' + ', '//cron expression is used to get an expression in the configuration file or database that is empty when directly specified
  5. ' cron_expr ' = ' */5 * * * * '//directly specify cron expression to get an expression in the configuration file or database is empty
  6. ),
  7. ' Run ' = = Array (
  8. ' FilePath ' = ' cron ',//directory where the file is located relative to AppPath
  9. ' filename ' = ' myclass.php ',//filename
  10. ' Class ' = ' MyClass ',//class name if only simple function can be empty
  11. ' function ' = ' clear_log ',//functions to execute
  12. ' params ' = = Array ()//parameters to be passed
  13. )
  14. );
  15. $cron _schedule[' clear_log ') = ...
  16. $cron _schedule[' create_sitemap ') = ...
5 adding the corresponding classes and functions/application/cron/myclass.php
    1. <?php
    2. Class MyClass
    3. {
    4. function Clear_log ($params = Array ())
    5. {
    6. Clean log
    7. }
    8. //.....
    9. }
6 adding/application/controllers/cron.php
    1. <?php
    2. Class Cron extends Ci_controller
    3. {
    4. Public Function Index ()
    5. {
    6. $this->load->library (' cron_schedule ');
    7. $this->cron_schedule->dispatch ();
    8. }
    9. }
7 Adding a system cron
    1. #crontab-E
    2. Add scheduled tasks as needed, such as:
    3. */5 * * * * php/var/www/ci210/index.php cron Index
    4. #service Cron Restart

Linux crontab timed task invoke CI framework PHP code

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.