Reputation: 944
I've read the following doc entries:
http://book.cakephp.org/2.0/en/console-and-shells.html
http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html
As well as this question:
Creating Cron Jobs in CakePHP 2.x
I'm having trouble trying to implement two cron job functions, one being the exact same as the Stack Overflow question listed above to send a test email. The other to simply inset a new row in my "crons" table. Neither works and I believe it is the way in which I am trying to call the cron jobs. I don't believe I am using the correct path.
Console/Command/CronShell.php
class CronShell extends AppShell {
public $uses = array('Cron');
public function trigger() {
$cron = array(
'Cron' => array(
'title' => 'Cron Test'
)
);
$this->Cron->create();
$this->Cron->save($cron);
}
}
I have set up a CronsController.php with the above code as part of the index action. The code works fine when accessed via the controller so the issue is with the shell or cron job.
I used to following commands to call this method as a cron job, none worked...
***** cd /home1/bhndbrwn/public_html/cake2/app && Console/cake cron trigger
***** cd /home1/bhndbrwn/public_html/cake2/app && Console/cake cronshell trigger
***** cd /home1/bhndbrwn/public_html/cake2/app && Console/cake Cron trigger
***** cd /home1/bhndbrwn/public_html/cake2/app && Console/cake CronShell trigger
***** cd /home1/bhndbrwn/public_html/cake2/app/Console/cake cron trigger
***** cd /home1/bhndbrwn/public_html/cake2/app/Console/cake cronshell trigger
***** cd /home1/bhndbrwn/public_html/cake2/app/Console/cake Cron trigger
***** cd /home1/bhndbrwn/public_html/cake2/app/Console/cake CronShell trigger
Similarly I tried the following shell to send a test email
Console/Command/EmailShell.php
App::uses('CakeEmail', 'Network/Email');
class EmailShell extends Shell {
public function main() {
$Email = new CakeEmail();
$Email->template('test', 'default')
->emailFormat('html')
->to([email protected])
->from('[email protected]')
->subject('Cron Email')
->send();
} // END MAIN FUNCTION
}
Again I tried the following commands. For each of these commands I also tried removing the method name "main" per the doc's instructions.
***** cd /home1/bhndbrwn/public_html/cake2/app && Console/cake email main
***** cd /home1/bhndbrwn/public_html/cake2/app && Console/cake emailshell main
***** cd /home1/bhndbrwn/public_html/cake2/app && Console/cake Email main
***** cd /home1/bhndbrwn/public_html/cake2/app && Console/cake EmailShell main
***** cd /home1/bhndbrwn/public_html/cake2/app/Console/cake email main
***** cd /home1/bhndbrwn/public_html/cake2/app/Console/cake emailshell main
***** cd /home1/bhndbrwn/public_html/cake2/app/Console/cake Email main
***** cd /home1/bhndbrwn/public_html/cake2/app/Console/cake EmailShell main
2017 EDIT - STILL NOT WORKING
I have updated my cron to /home/allfan5/public_html/allfans/app/Console/cake.php -app /home/allfan5/public_html/allfans/app/ test action
I have a shell called TestShell and an function called "action". The action function is completely empty to test things (I was also trying a function where I was emailing users but I was getting errors so I created a new shell and a completely empty function and I'm getting the same error).
The error I am receiving now is
2017-10-14 21:34:02 Error: Fatal Error (64): Cannot use ‘String’ as class name as it is reserved in [/home/allfan5/public_html/allfans/lib/Cake/Utility/String.php, line 25]
2017-10-14 21:34:02 Error: [FatalErrorException] Cannot use ‘String’ as class name as it is reserved
Stack Trace:
#0 /home/allfan5/public_html/allfans/lib/Cake/Error/ErrorHandler.php(203): ErrorHandler::handleFatalError(64, ‘Cannot use ‘Str…’, ‘/home/allfan5/p…’, 25)
#1 /home/allfan5/public_html/allfans/lib/Cake/Core/App.php(929): ErrorHandler::handleError(64, ‘Cannot use ‘Str…’, ‘/home/allfan5/p…’, 25, Array)
#2 /home/allfan5/public_html/allfans/lib/Cake/Core/App.php(902): App::_checkFatalError()
#3 [internal function]: App::shutdown()
#4 {main}
I have no idea what could be causing this as the shell function is completely empty. Even the action when I tried emailing out users, I copied the code and ran it from a controller and it worked fine. So there's something wrong with how Cake is executing or calling the shell.
I am running cake 2.5 on PHP 5.4
Upvotes: 8
Views: 2044
Reputation: 342
I am running cake 2.5 on PHP 5.4
You maybe running php 5.4 but your php-cli maybe in different version for example could be php 7 check your cli version. In php 7 you can't create class with name String.
* @package Cake.Utility
*/
class String {
Upvotes: 2
Reputation: 804
This method is the "vanilla" method, does not require you to edit or add any php files to dispatch your Cron jobs, it works out the box with CakePHP 2.x
So for CakePHP 2.x use this:
Settings:
You have a Shell called CustomShell.php located at /public_html/app/Console/Command/CustomShell.php
Inside this CustomShell.php you have several functions: functionThis()
and functionThat()
How to call this Shell and how to call each function as I please?
You go to your cPanel (or whatever manage you're using), inside the Cron Job Manager, and this is what you would type in the command:
The syntax looks like this:
cd /home/your_user/public_html/app && Console/cake shell_name function_name
This is how the cmd looks like with my example:
cd /home/your_user/public_html/app && Console/cake custom functionThis
Notice that the function name is not CustomShell
but custom
. Also, notice how I call the functionThis()
, you can leave this blank and it will automatically call your main()
function inside the CustomShell.php
file.
Upvotes: 2
Reputation: 815
Use the full path for the cake shell script (rather than using cd
) then pass the full path of your app directory, as per console instructions:
Your working path should be the same as your application path to change your path use the '-app' param. Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp
So your cron tab becomes:
* * * * * /home1/bhndbrwn/public_html/cake2/app/Console/cake -app /home1/bhndbrwn/public_html/cake2/app/ cron trigger
Upvotes: 2
Reputation: 45
We use a different way of running crons:
in the webroot dir I have a file called corn_dispatcher.php which is a copy of the index.php file but with some modifications to the end of the file:
App::uses('Dispatcher', 'Routing');
define('CRON_DISPATCHER',true);
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest($argv[1]), new CakeResponse());
Then I have a CronjobController.php file with the functions that are related to individual crons. Also added a beforeFilter function with $this->Auth->allow(); (so crons run without breaking due to ACL restrictions) as well as removing layouts and autorendering
Next set up routes to the crons.
Finally in my crontabs file I put:
1 6 * * * php -f /path-to-webroot/cron_dispatcher.php /routedUrl
Also few things to note:
I hope that helps.
Upvotes: 2