【发布时间】:2014-04-18 13:58:51
【问题描述】:
底部部分的 2017 年编辑
我已阅读以下文档条目:
http://book.cakephp.org/2.0/en/console-and-shells.html
http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html
还有这个问题:
Creating Cron Jobs in CakePHP 2.x
我在尝试实现两个 cron 作业功能时遇到了麻烦,其中一个与上面列出的用于发送测试电子邮件的 Stack Overflow 问题完全相同。另一个简单地在我的“crons”表中插入一个新行。两者都不起作用,我相信这是我试图调用 cron 作业的方式。我不相信我使用的是正确的路径。
控制台/命令/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);
}
}
我已经使用上述代码设置了一个 CronsController.php 作为索引操作的一部分。通过控制器访问代码时可以正常工作,因此问题出在 shell 或 cron 作业上。
我曾经按照命令将此方法称为 cron 作业,但没有任何工作...
***** 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
同样我尝试了以下shell来发送测试电子邮件
控制台/命令/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@domain.com)
->from('no-reply@domain.com')
->subject('Cron Email')
->send();
} // END MAIN FUNCTION
}
我再次尝试了以下命令。对于这些命令中的每一个,我还尝试按照文档说明删除方法名称“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
***** cd /home1/bhndbrwn/public_html/cake2/app/Console/cake Email main
***** cd /home1/bhndbrwn/public_html/cake2/app/Console/cake EmailShell main
2017 年编辑 - 仍然无法正常工作
我已将我的 cron 更新为 /home/allfan5/public_html/allfans/app/Console/cake.php -app /home/allfan5/public_html/allfans/app/ test action
我有一个名为 TestShell 的 shell 和一个名为“action”的函数。操作函数是完全空的,无法测试(我也在尝试一个函数,我正在向用户发送电子邮件,但我遇到了错误,所以我创建了一个新的 shell 和一个完全空的函数,我得到了同样的错误)。
我现在收到的错误是
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}
我不知道是什么原因造成的,因为 shell 函数完全是空的。即使是当我尝试通过电子邮件发送用户时的操作,我也复制了代码并从控制器运行它,它运行良好。所以 Cake 执行或调用 shell 的方式有问题。
我在 PHP 5.4 上运行 cake 2.5
【问题讨论】:
-
第一个(这个)我对路径感到困惑。第二个(那个)完全没有用。此外,这是针对旧版本的 Cake 所以不确定路径是否正确。我试图调整到 Cake 2.X 供应商路径,但仍然没有工作。根据文档,我想避免使用供应商目录,shell 应该放在 Console/Command 中。
-
@slywalker 那个文档很糟糕:|。 The official docs 不要误导用户认为您需要“特别的东西”来运行 cron 作业。 Bowlerae 如果您可以在 cli 上运行您的命令并且它不能通过 cron 工作 - 您需要说明如何/为什么 - 除了例如环境变量,与流行的看法相反,cron 与正常运行并没有什么不同。
-
脚本不是在 PHP 5.4 上运行,而是在 PHP 7.x 上运行。 book.cakephp.org/2.0/en/appendices/… | google.com/…
-
@ndm 谢谢。我怀疑这是问题所在,但我不认为迁移会这么容易。如果您提交正式答案,我会接受。
标签: cakephp cron cakephp-2.5