What does it do
This library sets up cron to perform tasks from crontab file. When crontab file is written with this library. Almost all you need is set up config/crontab.php file.Installation
- Download library from github repository git://github.com/biozshock/crontab.git
- If you are familiar with phpunit you can test library. Tests are located in tests subdirectory
- Copy config/crontab.php and libraries/crontab.php
- Setup config/crontab.php
- Use this library
Crontab library config file
- cronfile
- Path to cronfile library will write all jobs
- php_path
- Path to PHP executable
- crontime
- Crontab library default format for date() function
- cli_path
- PHP cli scripts path
- time_offset
- This config variable can be used when your server has different timezone from timezone of tasks you want to be cheduled. e.g. server is in -5 timezone. backend in +1 timezone. in backend you setting cronjob for 00:00:00, but for server this would be 14:00:00
Library usage examples
Simple usage
To execute cli script every day at midnight n your controller you should put following code:
$this->load->library('crontab');
$this->crontab->add_job('0 0 * * *', 'cli_script.php');
This code will execute cli_script.php located in directory you've set up in config file at 0 minutes 0 hour
Advanced usage
Assuming you have in your $_POST['time'] time when scheduled action should be performed.
$this->load->library('crontab');
$time = $this->crontab->translate_timestamp(strtotime($this->input->post('time')), 's i G * *');
$this->crontab->add_job($time, 'cli_script.php');
If you want to perform an action every day after user submit a data:
$this->load->library('crontab');
$time = $this->crontab->translate_timestamp(time(), 's i G * *');
$this->crontab->add_job($time, 'cli_script.php');
Warning
Please make sure:- your php exec is enabled
- your server user has access to crontab
- you know what you are doing
Good post keep it up.
ReplyDeleteAnd what if the script you want to execute is a controller?
ReplyDeleteYou are free to put any contents to a cli_script.php
ReplyDeleteOn my past project we included index.php (entry point) and in cli scipt you have a $CI variable. You can put a $_GET['/welcome/index'] = ''; before including index.php. This will load route /welcome/index/
Great lib. Thanks.
ReplyDeletecli_script.php please clear location of this file.
ReplyDeletegreat lib, saved my time...
ReplyDeletewhere i put this code that automatically run. as cpanel cron job
ReplyDelete$this->load->library('crontab');
ReplyDelete$this->crontab->add_job('0 0 * * *', 'cli_script.php');
this code put where that automatically run and hit url as like cron job.
because if i will put it inside of any controller then need to hit controller first. this function will not work automatically
dear all am so confused just including the library and putting those lines will get the job done whenever needed? how will you please make a detailed example...?
ReplyDelete