├── LICENSE ├── README.md └── cron.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright (c) 2009 by Jonathon Hill 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 7 | associated documentation files (the "Software"), to deal in the Software without restriction, 8 | including without limitation the rights to use, copy, modify, merge, publish, distribute, 9 | sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies or 13 | substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 16 | NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 19 | OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | codeigniter-cli-bootstrap 2 | ========================= 3 | 4 | Formerly: **Cron Bootstrapper** 5 | 6 | A simple bootstrap file that you can use to directly run your CodeIgniter controllers from the command line 7 | and redirect the output to a log file. 8 | 9 | Features 10 | -------- 11 | 12 | * Easy to use 13 | * No core framework hacking 14 | * Bootstrap file exists independently of your application 15 | * Supports runtime limit 16 | * Log the results of each run 17 | * Run directly without the PHP command (on UNIX systems only) 18 | 19 | Installation 20 | ------------ 21 | 22 | 1. Copy the code at the bottom of this code and save it in a file called **cron.php** somewhere outside the 23 | document root. 24 | 25 | 2. Set the `CRON_CI_INDEX` constant to the full absolute file/path of your CodeIgniter `index.php` file 26 | 27 | 3. Make this file directly executable: 28 | 29 | ```sh 30 | chmod a+x cron.php 31 | ``` 32 | 33 | Usage 34 | ----- 35 | 36 | You can use this file to call any controller function: 37 | ```php 38 | ./cron.php --run=/controller/method [--show-output] [--log-file=logfile] [--time-limit=N] [--server=http_server_name] 39 | ``` 40 | 41 | Options 42 | ------- 43 | 44 | ### `--run=/controller/method` 45 | (Required) The controller and method you want to run. 46 | 47 | ### `--show-output` 48 | (Optional) Display CodeIgniter's output on the console (default: don't display) 49 | 50 | ### `--log-file=logfile` 51 | (Optional) Log the date/time this was run, along with CodeIgniter's output 52 | 53 | ### `--time-limit=N` 54 | (Optional) Stop running after N seconds (default=0, no time limit) 55 | 56 | ### `--server=http_server_name` 57 | (Optional) Set the `$_SERVER['SERVER_NAME']` system variable (useful if your application needs to know what the 58 | server name is) 59 | 60 | Troubleshooting 61 | --------------- 62 | 63 | ### `Invalid interpreter: /usr/bin/php^M` error when running cron.php: 64 | 65 | This is caused by not having UNIX-style line breaks, which usually happens by copying files created 66 | on other operating systems. Use this command to convert the line breaks to UNIX format: 67 | 68 | ```sh 69 | mv cron.php cron.old 70 | tr -d '\15\32' < cron.old > cron.php 71 | rm cron.old 72 | ``` 73 | 74 | ### Fatal error in cron.php around line 111: 75 | 76 | ```php 77 | require(CRON_CI_INDEX); // Main CI index.php file 78 | ``` 79 | 80 | Check that you have correctly defined the path to your application's main **index.php** defined correctly: 81 | 82 | ```php 83 | define('CRON_CI_INDEX', '/var/www/vhosts/htdocs/index.php'); // Your CodeIgniter main index.php file 84 | ``` 85 | 86 | ### No errors or output and the script doesn't seem to run: 87 | 88 | Make sure PHP error logging is turned on that your PHP error log is writable. 89 | 90 | Code that depends on sessions may cause this. Use some conditional logic to see if the `CRON` constant 91 | is defined before auto-loading any authentication libraries. 92 | 93 | -------------------------------------------------------------------------------- /cron.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/php 2 | FALSE); 33 | $uri = ''; 34 | 35 | foreach($argv as $arg) 36 | { 37 | list($param, $value) = explode('=', $arg); 38 | 39 | switch($param) 40 | { 41 | case '--run': 42 | // Simulate an HTTP request 43 | $uri = $value; 44 | $_SERVER['PATH_INFO'] = $value; 45 | $_SERVER['REQUEST_URI'] = $value; 46 | $_SERVER['SERVER_NAME'] = 'localhost'; 47 | $required['--run'] = TRUE; 48 | break; 49 | 50 | case '-S': 51 | case '--show-output': 52 | define('CRON_FLUSH_BUFFERS', TRUE); 53 | break; 54 | 55 | case '--log-file': 56 | if(is_writable($value)) define('CRON_LOG', $value); 57 | else die("Logfile $value does not exist or is not writable!\n\n"); 58 | break; 59 | 60 | case '--time-limit': 61 | define('CRON_TIME_LIMIT', $value); 62 | break; 63 | 64 | default: 65 | die($usage); 66 | } 67 | } 68 | 69 | if (!defined('CRON_FLUSH_BUFFERS')) 70 | { 71 | define('CRON_FLUSH_BUFFERS', FALSE); 72 | } 73 | 74 | $_SERVER['argv'][1] = $uri; 75 | 76 | for ($i=2; $i<$argc; $i++) 77 | $_SERVER['argv'][$i] = ''; 78 | 79 | if( ! defined('CRON_LOG')) 80 | { 81 | define('CRON_LOG', 'cron.log'); 82 | } 83 | 84 | if( ! defined('CRON_TIME_LIMIT')) 85 | { 86 | define('CRON_TIME_LIMIT', 0); 87 | } 88 | 89 | foreach($required as $arg => $present) 90 | { 91 | if( ! $present) 92 | { 93 | die($usage); 94 | } 95 | } 96 | 97 | // Set run time limit 98 | set_time_limit(CRON_TIME_LIMIT); 99 | 100 | // Run CI and capture the output 101 | ob_start(); 102 | 103 | chdir(dirname(CRON_CI_INDEX)); 104 | require(CRON_CI_INDEX); // Main CI index.php file 105 | $output = ob_get_contents(); 106 | 107 | if(CRON_FLUSH_BUFFERS === TRUE) 108 | { 109 | while(@ob_end_flush()); // display buffer contents 110 | } 111 | else 112 | { 113 | ob_end_clean(); 114 | } 115 | 116 | // Log the results of this run 117 | error_log("////// ".date('Y-m-d H:i:s')." cron.php $cmdline\r\n", 3, CRON_LOG); 118 | error_log(str_replace("\n", "\r\n", $output), 3, CRON_LOG); 119 | error_log("\r\n////// \r\n\r\n", 3, CRON_LOG); 120 | 121 | echo "\n\n"; 122 | 123 | --------------------------------------------------------------------------------