Command Line Cron Job For Codeigniter (Continued)

I showed how to make a file for setting cron job handled by command line  (  Click Here To see the previous post ). There are a limitation of that way which is “for every cron we should make a new file“. Here we will see how can we make a common file where any amount of cron can be handled by the file.

# /cron/cron.php
ini_set('memory_limit', '256M');
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');
/* set some constants */
define('CMD', 1);
$_SERVER['QUERY_STRING'] =  $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '/' . implode('/', $argv) . '/';

Here we are using PHP’s $argv variable to get a list of the arguments,  so if I called the script by saying:

-f path/cron.php cron sendmail

…then $argv would be an array with the elements of “cron.php”, “cron”, “sendmail”. We don’t need the first element, so  unset that. Then implode the rest of the elements to form the request path, and the end result would be /cron/sendmail/, same as the before.

So you just need to call the file like follows:

/usr/bin/php -f /home/user/public_html/cron/cron.php controller_name function_name

Hope you are clear about the idea.

Thank You.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: