Kohana requests from the Command Line

Posted . Visible to the public.

Kohana 3.2 has support for cli (command line interface), and can be used like this:
php -q /path/to/kohana/site/index.php --uri=cron/update_demo_data
This example assumes there is a Controller_Cron with an action_update_demo_data()

To set this up as a cron job, edit the crontab with something like this using the desired time settings
1 0 * * * /usr/bin/php -q /path/to/kohana/site/index.php --uri=cron/update_demo_data`
This example runs every day at 12:01am

In cases like the example above with having a Controller_Cron, it may be wise to lock down requests only to the command line (to prevent requests from the browser). This can easily be done by adding a hook in the before() method of the controller or in any specific action using Kohana::$is_cli
public function before()
{
if(!Kohana::$is_cli){
throw new HTTP_Exception_404('The requested page does not exist!');
}
}

For executing a command line request within Kohana or another PHP script (perhaps for forking) use this:
exec('/usr/bin/php /path/to/kohana/site/index.php --uri='.$request.' > /dev/null &', $errors, $response);
Kohana 3.2 supports internal requests via HMVC Show archive.org snapshot , so the command above may not be practical from within the Kohana Framework

Profile picture of Matt Tew
Matt Tew
Last edit
Tags
Posted by Matt Tew to Matt Tew's deck (2012-03-23 15:29)