I am trying to set up a cron job using COdeigniter but I cannot figure out how to get it to work. I have a file called email_check.php in my controllers folder, and I have added a .cron file to the servers cron folder, which contains the following
email_check.cron
*/1 * * * * php /var/www/html/application/controllers/email_check
email_check.php
class Email_check extends CI_Controller {
function __construct()
{
parent::__construct();
$this->index();
}
function index()
{
$this->load->model('admin/info_model');
$this->info_model->addTestData();
}
}
The addTestData adds a new row into a database table. I would like this to run every minute, however it isn't working at all and I have no idea why.
Maybe it may be the paths that are wrong. Do I need to point the php part to the php.exe in the server.
If anyone could help or point me in the right direction it would be greatly appreciated!
To use CodeIgniter via the commmand line, you need to call the index.php file and pass in the controller and method as arguments, plus any other arguments. So at the minimum the cron job call would be:
~/public_html/sitefolder/index.php controller method
Or using the path to your application index file. But, you also need to use PHP compiled for the command line, not just PHP for CGI-FCGI. So your call might be something like:
/ramdisk/bin/php5-cli ~/public_html/sitefolder/index.php controller method
Depending on where your PHP CLI is located.
This won't work because just hitting your email_check.php controller won't do anything because its not going to call your index() method.
You want to either write a script that is going to create a new instance of the controller and call the method or call it via a URL, something like this I think
* * * * * wget http://sitename.com/email_check
Related
My sidebar has elements that are the same in all my views, so instead of calling them over and over in all my controllers, I created a MY_Controller class and make variables available from there. Problem is, the content from my variables come from a file, so now everytime my site is loaded I'm making a call to read the file. It was pointed out to the me that this is not too good.
So what I'm trying to do is read the file in a model, then store it in a variable and then pass that variable to MY_Controller. Problem is I'm facing the same issue only now instead of reading the file from MY_Controller I'm reading it from the model. So I tried to create in a different function a variable that would be available in my entire model, and then a function that returns that variable, but it is not working. Here is my code:
Model:
class My_model extends CI_Model {
public $report = null;
function read_file()
{
$this->report = file_get_contents('/path/to/file');
}
function get_file()
{
return $this->report;
}
}
MY_Controller:
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('My_model');
$report = $this->My_model->get_file();
...
}
}
Right now $reportis returning null.
The read_file() function should be called once a day, because the file is updated daily in the morning. Any tips pointing if it is possible to do what I'm trying, or if there is a better way would be greatly appreciated.
I'm not going to give you code, because it would take too long, but I will explain how Symfony2 does it. Heard of Twig? Well, regardless of whether you are familiar with it or not, the important part of it is how it compiles templates.
It compiles all template logic and text to PHP files filled with generated functions and strings. The first time the template is executed this compilation is performed. The second time, the compiled PHP is re-used.
The IO in effect still takes place, but only on the included PHP pages. You can eliminate this again by bootstraping your entire source into a single file (Symfony does this with libraries, but not templates).
So, in order to "cache" your content to PHP, you need to write a PHP dumper (or using an existing one), or use var_export on simple data structures to dump your pre-loaded data.
You should call read_file function before get_file, also there is no relation between calling the function read_file and the updating of the file.
I have my own library class which is designed for gathering data from external web sites (uses curl). This class has constructor which receives parameters (e.g. URL, xpaths) and method updateDatabase. How to pass parameters to constructor and call method updadeDatabase in app internally? This method should be fired e.g. two times per day without user request (using the cron) So I don't want insert this code in controller and create appropriate route. How to do this?
class Source
{
public function __construct(Http $http, array $params)
{
...
}
public function updateDatabase()
{
...
}
}
Your best bet is probably to create an artisan command. This gives you access to to run your code within the Laravel framework from the command line (or cron job).
All the information you need for creating an artisan command can be found here.
Basically, inside the command's fire() method you would do something like:
public function fire() {
$source = App::make('Source'); // or however you instantiate your class
$source->updateDatabase();
// output a message to the command line
$this->info('All done.');
}
Your command can also take arguments and options from the command line, if you need to pass those in.
The documentation linked above will tell you all you need to know.
How can I call a shell from my component.
Here is my shell
<?php
class PrintShell extends AppShell {
function main()
{
App::import(‘Component’, ‘BusinessLogic’);
$businessLogic = & new BusinessLogicComponent();
$businessLogic->initialize();
$settings = $businessLogic->senReminderEmail();
// senReminderEmail Mail function is defined in my Controller/Components/BusinessLogicComponent.php File
}
}
?>
I dont want it to call using commands.
How can I do this.Any help
Does this makes sense?
you want to call shell
i think, do you want to use cron job in cakephp..?
mostly appshell used to work with cron job and i have also faced out this one and i had not used appshell i set up my cron job from godaddy server and putted all files in webroot folder which may operate during cron time so i just verified..dear..
I'm doing a huge project with Symfony2. Frontend is javascript/html5 canvas. For site changes I use ajax requests.
Now I want to call a php file which should be executed with EVERY user action. Is there an elegant way to do so?
For better understanding: I'm doing some kind of game and the php checks, if something happend (recruitments done, buildings finished etc.).
Take a look into JMSAopBundle
If by user action you mean executing a controller action (i.e. server side), what I would do is listen to the kernel.controller event: http://symfony.com/doc/current/book/internals.html
so you can load your script and execute it juste before the target controller is invoked
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
//...
// call your php file here
// the controller can be changed to any PHP callable
$event->setController($controller);
}
I don't think you even need to modify the controller so you can remove the first and last line...
I wrote a shell method in CakePHP 1.3 that has a return value.
I'd like to be able to access that method from within a controller, so that I can pass its return value into the View.
I'm not sure how to access those methods appropriately from within the controller. Have I done it wrong?
I could easily duplicate the code, but I'd like to "keep it DRY", and the actual functionality, I believe, doesn't belong with this particular controller - I just need it's return value in this particular view.
EDIT:
I realize I'm sort of asking the wrong question here, since the Shell itself shouldn't necessarily return a value. I've changed the code so that the Shell is only using the return value I want, and now I wonder - what is the appropriate place for extra classes/code that needs to be accessed from a Shell and a Controller?
It seems like Component code, but I'm not sure how to access Components from the Shell. It's not a Plugin, as I understand them. Where does this go?
In one of the projects we imported shell tasks, ex:
App::import('Core', 'Shell');
App::Import('Vendor','shells/tasks/sometask');
$returndata = TasknameTask::execute($somevalue);
You can create a component to do that.E.g
/* in app/controllers/components */
class ShellComponent extends Object
{
function do_shell()
{
return shell_exec('some command');
}
}
Then use it in any controller you want as below
/* in some controller*/
var $components = array('Shell','maybe some other components',....);
function testShell()
{
$result = $this->Shell->do_shell();
....
}
Shells shouldn't directly return a value explicitly, they ought to report it somehow, e.g. by echoing it to stdout, logging to a file or sending an email for example. I like to think of shells as controllers for the cli.
Without knowing your application, my suggestion would be to see if you could refactor the logic in your current shell into a model class or something like that, have the model method return the value, then use that model in your shell. This way, you can also use that model in your controller.
The accepted answer doesn't seems to work for Cake 2.0
For Cake 2.0
if (!class_exists('Shell')) {
require CONSOLE_LIBS . 'shell.php';
}
App::import('Shell', 'DoSomething');
DoSomethingShell::main();
Bear in mind some Shell method doesn't work in this mode, such as $this->out, so more hacking is required.