Welcome Guest, Not a member yet? Register   Sign In
How to use Cron with Codeigniter to query a MySQL database?
#1

[eluser]tim peterson[/eluser]
Hi all, i'm interested in what the code (command line and php) would look like where every minute or hour you take inventory of the number of item sales obtained from a mysql database and do some action based on that. I'm using codeigniter for php and i'll call my controller "cronControl".

Here's what I have so far for the cronControl Controller:

Code:
class CronControl extends CI_Controller {

  function countSales(){
     $count=$this->db->query("SELECT stuff");
     //do->stuff->based on $count;
     $count="i counted 137 items";          
     file_put_contents("mylogfile.txt", $count);
  }
}
when I type the following into shell:
Code:
* * * * * php index.php cronControl countSales
i get:
Code:
-bash: 404.php: command not found
It looks like it is evaluating all the php scripts in my root directory (where my 404.php page is) and not just the cronControl/countSales controller. Please note that this shell command works and prints $count to mylogfile.txt if you leave out the 5 asterisks.

any thoughts on what's going on?

thank you,

tim
#2

[eluser]CroNiX[/eluser]
Did you try setting up an actual cron job with crontab or are you just entering that (with the asterisks) straight into the command prompt?
#3

[eluser]tim peterson[/eluser]
Hi Cronix,

i tried both ways, both didn't work, think there is something going on here with paths i'm specifying. If it is relevant, i've written the .htaccess rules so I can just type, http://localhost/cronControl/countSales and not http://localhost/index.php/cronControl/countSales.

thanks,
tim
#4

[eluser]CroNiX[/eluser]
Try using a full path to your php and also a full path to your index.php. Also, the -q flag might be necessary.

/usr/bin/php -q /home/site/index.php controller method
#5

[eluser]tim peterson[/eluser]
Hi CroNIX,

unfortunately, these aren't working either. my index.php is in my htdocs folder whereas my CI app is in the application folder

i'm on a mac and here's what i mean:

/applications/xampp/htdocs/index.php
/applications/xampp/htdocs/application

thanks,
tim
#6

[eluser]CroNiX[/eluser]
Don't know what else I can do to help. I only use my mac to test websites with the browser so I'm not sure how to advise you on setting up cron jobs for a mac. What I posted works on my production servers, which are Ubuntu and CentOS.

Have you set up other cron jobs for your mac? Maybe they have some unorthodox settings. I'd try google to see about cron jobs on a mac in general. It could also be a permissions issue.
#7

[eluser]tim peterson[/eluser]
ok i figured it out.

the key when typing the command is in the crontab file to include the asterisks, but in the shell to NOT include the asterisks

so crontab -e:
* * * * * /usr/bin/php /applications/xampp/htdocs/index.php cronControl countSales

whereas in htdocs $
/usr/bin/php /applications/xampp/htdocs/index.php cronControl countSales

thanks,

tim
#8

[eluser]CroNiX[/eluser]
The asterisks determine what time/day to run the cron job on a schedule. When running from the command line, there is no scheduled time needed as you are just executing a command on the command line for immediate execution.
#9

[eluser]tim peterson[/eluser]
ok thanks, i think i now understand!

thanks again,

tim




Theme © iAndrew 2016 - Forum software by © MyBB