Part of the EllisLab Network
This thread is a discussion for the wiki article: Cron job bootstrapper
   
1 of 5
1
Cron job bootstrapper
Posted: 19 August 2008 05:41 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  74
Joined  02-26-2008

Here is a very simple bootstrapper file that you can use to directly run your CodeIgniter controllers from the commandline. It’s a very easy and elegant solution for using CI controllers for cron jobs. It also logs the date/time and output each time it runs.

Download

Please let me know if you find this useful, or have an idea of how to improve this further.

Example:

cron.php --run=/controller/method [--show-output] [--log-file=logfile] 

The default logfile is cron.log, but you can change that with the—log-file switch. It does not show the output by default, but you can force it to using the -S or—show-output switch.

 Signature 

[ Cron bootstrapper ] [ Modularity ] [ Sitemaps ] [ Query Browser ] [ Paging Simplified ] [ jonathonhill.net ]

Profile
 
 
Posted: 06 September 2008 07:41 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  146
Joined  01-02-2008

Nice wink
I’ll let you know if I write any improvements.
-Matt

Profile
 
 
Posted: 08 October 2008 09:28 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  2
Joined  08-26-2008

Hi, I’m a bit new to all this, but I need cron to set up an autoresponder, and this looks like a great way of doing it. Where should I put this file in my CI setup, or does it go in the root of the domain?
Thanks.

Profile
 
 
Posted: 08 October 2008 10:07 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  74
Joined  02-26-2008

For security you should put it anywhere outside of your document root (you don’t want web users to be able to access it). Then to get it working all you have to do is:

1) Set the CRON_CI_INDEX constant to the location of your CodeIgniter index.php file
2) Make this file executable (chmod a+x cron.php)

Then you can run it directly (./cron.php [parameters]), and if you get a “missing PHP interpreter^M” error convert your line breaks to UNIX format (see http://www.tech-recipes.com/unix_tips200.html). The instructions on the Wiki are pretty clear.

 Signature 

[ Cron bootstrapper ] [ Modularity ] [ Sitemaps ] [ Query Browser ] [ Paging Simplified ] [ jonathonhill.net ]

Profile
 
 
Posted: 17 November 2008 07:48 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  06-26-2007

Severity: Notice —> Use of undefined constant CRON_FLUSH_BUFFERS - assumed ‘CRON_FLUSH_BUFFERS’ .../cron.php 86

If you don’t like this message, change

if(CRON_FLUSH_BUFFERS === TRUE

to

if(defined('CRON_FLUSH_BUFFERS') and CRON_FLUSH_BUFFERS === TRUE
 Signature 

owner of http://www.bdteam.hu

Profile
 
 
Posted: 17 November 2008 07:50 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  06-26-2007

And thanx Jonathon Hill for this solution!

 Signature 

owner of http://www.bdteam.hu

Profile
 
 
Posted: 17 November 2008 10:29 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  74
Joined  02-26-2008
BDT - 17 November 2008 12:50 PM

And thanx Jonathon Hill for this solution!

Glad you found it helpful!

 Signature 

[ Cron bootstrapper ] [ Modularity ] [ Sitemaps ] [ Query Browser ] [ Paging Simplified ] [ jonathonhill.net ]

Profile
 
 
Posted: 25 December 2008 06:43 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Avatar
Total Posts:  9
Joined  08-04-2008

Jonathan, I just pulled your bootstrapper into my app and really like it. The main reason I like it is because I can use this one file to control all of my different cron jobs. I have one question though. For some reason, everything dies after the following is called:

require(CRON_CI_INDEX); // Main CI index.php file 

It never gets to the point of actually writing to the log files, as that code is just below. I have no exits or die statements in the called script, and my script runs just fine and produces the correct output. Any thoughts on why this could be happening?


Also, I updated a few things that may be of use to others:

I have a switch statement in my /system/application/config/config.php file that checks to see what server I’m on so that certain machine specific settings are set. So, I added a server switch so you can run:

cron.php --run=/controller/method [--show-output] [--log-file=logfile] [--server=http-server] 

The code I added for this is:

case '--server':
      
$_SERVER['SERVER_NAME'$value;
      
$required['--server'TRUE;
break; 

I also updated the $CI_Root so that it would be server-independent (note the the bootstrap file lives in a scripts directory at the same level as application).

$CI_Root dirname(__FILE__).'/../../index.php'
Profile
 
 
Posted: 25 December 2008 08:12 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Avatar
Rank
Total Posts:  74
Joined  02-26-2008

Thanks electromute for the—server idea! I have added it to the script along with a credit.

If you’re getting errors at this line:

require(CRON_CI_INDEX); // Main CI index.php file 

Then you must not have the CRON_CI_INDEX constant set correctly. It has to be set to the exact full absolute server path and filename of your CI app’s main index.php file. include() and require() don’t work the same way when called from the command line as they do when called in a web page.

BTW I’ve updated the wiki page with a minor code update and some installation and troubleshooting information.

 Signature 

[ Cron bootstrapper ] [ Modularity ] [ Sitemaps ] [ Query Browser ] [ Paging Simplified ] [ jonathonhill.net ]

Profile
 
 
Posted: 26 December 2008 04:35 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Avatar
Total Posts:  9
Joined  08-04-2008

Thanks, Jonathan.

Yes, I had the path to my index.php fine, my cron was running and the script was executing just fine.

But I did have an “exit” hiding in the remap function (like so). I don’t use the remap very much so I kind of ignored it a bit.

function _remap($action){
        $this
->$action();
        exit;
    

(bad, bad exit!)

Ok, New Year’s resolution is to use die(“some indication that it died”); rather than silent exits.

—ingrid

Profile
 
 
Posted: 05 January 2009 02:51 AM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  467
Joined  01-17-2008

awe man right when i was going to roll up my sleeves and figure out how the hell i was going to do this you come along and solve all my problems…. back to being lazy smile

thanks again man this is awesome

 Signature 

aka trs21219
CodeSanity | Github | LinkedIn | Facebook | Twitter | Last.fm

Profile
 
 
Posted: 11 February 2009 10:31 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
RankRank
Total Posts:  191
Joined  11-09-2007

2) Make this file executable (chmod a+x cron.php)

How do you make it executable? Do you need SSH/Telnet or something?

thanks

Profile
 
 
Posted: 12 February 2009 02:23 PM   [ Ignore ]   [ # 12 ]  
Summer Student
Avatar
Total Posts:  9
Joined  08-04-2008
gh0st - 11 February 2009 03:31 PM

2) Make this file executable (chmod a+x cron.php)

How do you make it executable? Do you need SSH/Telnet or something?

thanks

Yes, chmod a+x cron.php is the command to run via a console interface. If this file is on a remote server, you’ll want to ssh and run this. You can sometimes also do this through your FTP program—some of them have options to change file permissions through their interface.

If you are on your own computer, you can usually fire up a console (Cygwin for windows or native on MacOS).

Hope that helps!

Profile
 
 
Posted: 27 February 2009 03:48 PM   [ Ignore ]   [ # 13 ]  
Summer Student
Total Posts:  9
Joined  11-06-2008

this worked perfectly!  great job!

thank you SO MUCH.

Profile
 
 
Posted: 05 March 2009 08:00 PM   [ Ignore ]   [ # 14 ]  
Grad Student
Rank
Total Posts:  31
Joined  08-09-2008

Hi for all. I am tryibg to do work the boostrapper but I have a problem. When I execute by cronjob (code) in output it shows the 404 error. Why? The controller name and the function (code 2) is correct.


In my config file (code 3) I omit the index.php - maybe this is the problem?

/usr/bin/php -/home/domain.com/html/cron/cron.php --run=/paginas_controller/gerador 
/home/webadmin/domain.com/html/index.php 
$config['index_page'""
Profile
 
 
Posted: 11 March 2009 02:19 PM   [ Ignore ]   [ # 15 ]  
Lab Assistant
RankRank
Total Posts:  191
Joined  11-09-2007

I get the following errors.

I renamed the cron.php bootstrapper to ci_cron.php

I renamed the CI bootstap file to ci.php because I’m only using something else for my main site.

My system folder is not in the public folder (www) but sits one level up outside of web root.

# Output from shell prompt;

someuser@somehost [~/www]# php ~/mycronjobs/ci_cron.php—run=/ci.php/cron/run/—show-output

Warning: chdir(): No such file or directory (errno 2) in /path/to/mycronjobs/ci_cron.php on line 110

Warning: require(/path/to/system/www/ci.php): failed to open stream: No such file or directory in /path/to/mycronjobs/ci_cron.php on line 111

Fatal error: require(): Failed opening required ‘/path/to/system/www/ci.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /path/to/mycronjobs/ci_cron.php on line 111

I figured it out—I set the path to the ci.php file incorrectly!  It should be /path/to/www/ci.php and not /path/to/system/www/ci.php!

Thanks

Profile
 
 
   
1 of 5
1
 
‹‹ PayPal Lib      css ››