0

I have a CakePHP script that should hopefully be run by a cron job. It runs fine from the command line, but seemingly not from the cron. The cron line is something like:

*/2 * * * * cd /path/to/app;../cake/console/cake do_update

The script itself - and this is the bit that I think may possibly be too wacky, to the extent of throwing off the cron - loops through a subset of a Realtors table in the database, using the system time to decide which 50-record slice of the database to update:

$realtors = $this->Realtor->find('all',array(
'conditions'=>array('Realtor.zone_id'=>1),
'order'=>array('Realtor.num DESC'),
'limit'=>50,
'offset'=>date("i")*25
));

So my question(s) is/are - is there anything I'm doing here that would obviously throw the cron job for a loop? And, perhaps more importantly, is my method of splitting a database into manageable chunks over the course of an hour crazy? (I'm pretty much a programming newbie, so I try a lot of things without knowing whether they're good practice or not.) Can anyone suggest a better way of looping through and updating large numbers of database records via a cron, that prevents the individual queries from being too huge for the system to handle?

EDIT: not only does it always work from the command line, it also works when run by cron script on a different server. I guess there's just something messed up on the particular server, so it seems doubtful there's a code-related solution! I'll just accept an answer from among the useful cron-related insights below...

thesunneversets
  • 2,560
  • 3
  • 28
  • 46

4 Answers4

0

I'd try to put "cd /path/to/app;../cake/console/cake do_update" in bash script and run the script in cron instead. But I'm not sure if it'll help.

Kamil Szot
  • 17,436
  • 6
  • 62
  • 65
0

Generally, you'd want to put all your command line stuff into an shell file

/path/to/cake/console/cron.sh

#!/bin/sh
cd /path/to/app
php ../cake/console/cake do_update
*/2 * * * * sh /path/to/cake/cron.sh

Normally I don't care about where the application actually starts (if i'm not using files) and just do

*/1 * * * * php /my/path/my_php.php
Rahly
  • 1,462
  • 13
  • 16
0

Try following the manual, it's working great for me:
http://book.cakephp.org/view/1110/Running-Shells-as-cronjobs

deceze
  • 510,633
  • 85
  • 743
  • 889
-1

I'm not that familiar with cron, but I didn't think you could run two commands together like that.

You might want to try:

*/2 * * * * cd /path/to/app && ../cake/console/cake do_update

This will cause the two commands to run together (since each command does return a number to indicate success or failure, and in the event of failure, an error code)

I am assuming your PHP file also includes the necessary #! line at the start pointing to the PHP interpreter, and that the file permissions allow for execution from the command line? (i.e. you can just literally run that statement above and it works)

Arantor
  • 597
  • 4
  • 15
  • Considering that it doesn't work on my production server without the shebang, and that every single scripting language does exactly the same... I think you do need the shebang unless the file is actually a binary executable file. I did say, up front, that I didn't know cron so well, and I'm guessing that cake - being a CakePHP built file - does actually have the shebang in it? (That's called due diligence in checking.) – Arantor Nov 25 '10 at 01:46
  • @sberry Shebangs work just fine for PHP scripts, it's not different from any other scripting language here. It's just used less often for PHP than for, say, Perl scripts. – deceze Nov 25 '10 at 01:49