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
<?php
set_time_limit(0);
ini_set('memory_limit', '256M');
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');
/* set some constants */
define('CMD', 1);
unset($argv[0]);
$_SERVER['QUERY_STRING'] =  $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '/' . implode('/', $argv) . '/';
include(dirname(dirname(__FILE__)).'/index.php');
?>

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.

Advertisements
  1. Leave a comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

w

Connecting to %s

%d bloggers like this: