How to Run Cron Jobs in CodeIgniter


After a fair bit of trawling Google, I got the impression that setting up a cron job in codeigniter was going to be a bit tricky.
The two potential solutions I discovered were:
  1. execute the “wget” command to replicate a normal browser request, which then exposes my cron functionality to the big wide world and creates some additional unnecessary overheads, or
  2. Create a bespoke front controller a bit like the existing index.php but specifically for cron jobs
Neither of these options sound particularly ideal to me, and thankfully, there is a better way (and it turned out to be a case of RTFM).
Firstly, just create a normal controller. I creatively called mine Cron.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Cron extends CI_Controller {
 
    function __construct()
    {
        parent::__construct();
 
        // this controller can only be called from the command line
        if (!$this->input->is_cli_request()) show_error('Direct access is not allowed');
    }
 
    function foo($bar = 'bar')
    {
        echo "foo = $bar";
    }
}
Note the call to $this->input->is_cli_request() which ensures that this controller cannot be accessed directly from a url.
Next we setup our cron job as you would any other, except that you provide the path in a special format.
php /path/to/index.php controller_name method_name ["params"]
The first part is either the command “php” or your server path to php.
The second part is the absolute path to your CI front controller, eg. /path/to/index.php.
The third part is the controller name, followed by the method name, followed by optional parameters.
The CI manual gives the example:
php /path/to/index.php cron foo
If that doesn’t work, it probably means you don’t have the package php5-cli installed. On debian / ubuntu you can install this package as follows:
sudo apt-get install php5-cli
If you are not able to install packages, you can specify your path to php. I set mine as follows:
/usr/local/bin/php -f /home/clinic/public_html/index.php cron foo
You can even pass parameters in quotes, which have the same effect as parameters in regular url requests!
/usr/local/bin/php -f /home/clinic/public_html/index.php cron foo “beer”

2 comments:

  1. اما ممکن است این سوال به ذهنتان بیاید که یک خرید هاست خرید هاست اشتراکی ارزان ارزان چه هاستی است و چه ویژگی هایی باید دارا باشد در ادامه مقاله قصد داریم ویژگی های هاست ارزان را خرید هاست لینوکس ماهانه معرفی کنیم و در آخر به معرفی بهترین هاست ایران از میان هاستینگ های برتر کشور بپردازیم .اگر قصد راه اندازی یک سایت باکیفیت و پر بازدید را دارید حتما باید نکته هایی را رعایت نمایید قسمت سخت افزار از انتخاب ها تشکیل می شود و مناسب برای سایت هست که به سرور استفاده شده با هاست ارزان برای وردپرس سی پی یو ، رم و استفاده از دیگر سخت افزارها هاست ارزان خارجی با ویژگی های عنوان شده مربوط می شود .نرم افراز به دو بخش جدا تقسیم می شود که بخشی از آن به هاست و سرور مربوط هست و بخش دیگر به سیستم مدیریت محتوا سایت شما بستگی دارد .و در نهایت بخش محتوا هاست پرسرعت ارزان ارتباط با کار سئو و بهینه سازی سایت برای موتورهای جستجو و هم چنین بازاریابی از طریق محتوا است .در حقیقت هاست یک بستر الکترونیکی هست که محتویات و مطالب سایت شما داخل آن ذخیره خواهد شد .و این محتویات هاست ارزان قیمت همان فایل و دیتابیس شما است که از طریق اتصال اینترنتی کاربران به سرور در دسترس خواهد بود .لازم است که هاست و دامنه به یک دیگر متصل باشند که این فرآیند از طریق Name server صورت می گیرد .بدین ترتیب اولین کاری که بعد از خرید هاست از شرکت های معتبر هاستینگ باید انجام دهید این هست که نیم سرور های هاست را بر روی دامنه خودتان ست کنید تا نهایتا بعد از گذشت ۷۲ ساعت هاست و دامنه به هم دیگر متصل شوند

    ReplyDelete
  2. Crontab files are simple text files that have a particular format. Each line of a crontab file follows a particular format as a series of fields, separated by spaces and/or tabs. Each field can have a single value or a series of values. A single cron job should take up exactly one line, but this can be a long line (more than 80 characters).
    for more information click here:how to setup a cron job

    ReplyDelete