Setting Cron Job From Command Line With Codeigniter

Most of the web application need to run some scripts to maintain the system time by time. In order to do that we use Command Line Request for running the scripts/files. But With Codeigniter it is not possible as it doesn’t have this functionality.  So how to call a controller/method from the command line we will learn here. A separate file needs to be created (eg: /cron/mycron.php) which will set the REQUEST_URI / PATH_INFO those are Environment variables used by CI.  CI use these variables to check which controller/method to call. We can see with an Example.

if (isset($_SERVER['REMOTE_ADDR'])) die('Called from Browser');
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '/home/my_cron'; // Setting the path of controller/method
include(dirname(dirname(__FILE__)).'/index.php'); //Now just call the framework

We will not allow to call the script from web browser. Then we set the ontroller/method to call. And at last we call the framework.

Drawbacks : One problem here to use this method. For every cron we should make a new file as the path of the controller/method cannot be changed. There are also some better approach if you have much more cron job request. At this time I need to set just 2 cron so I am using this. Will come with other method soon.

, , , , ,

  1. #1 by jocur i gratis on July 18, 2013 - 12:46 PM

    Hello I am so happy I found your blog page, I really found
    you by error, while I was looking on Digg for something else, Anyways I am here now and would just like to say kudos for a
    marvelous post and a all round entertaining blog
    (I also love the theme/design), I don’t have time to go through it all at the moment but I have book-marked it and also
    added in your RSS feeds, so when I have time I will
    be back to read a great deal more, Please do keep
    up the fantastic b.

  2. #2 by Lavonne on July 31, 2013 - 3:18 PM

    I really like your blog.. very nice colors & theme. Did you
    create this website yourself or did you hire someone to do it for you?
    Plz answer back as I’m looking to construct my own blog and would like to know where u got this from. thank you

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: