在CakePHP 2.x中创建Cron作业 [英] Creating Cron Jobs in CakePHP 2.x

查看:108
本文介绍了在CakePHP 2.x中创建Cron作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的CakePHP 2.x应用程序中创建cron作业。但是我在线阅读的所有资源似乎都是完全不同的,没有一致性,或用非常复杂的术语解释。



基本上我已经创建在 / app / Console / Command

中的 MyShell.php 下面的文件

 <?php 

class MyShell extends Shell {

public function sendEmail(){

App :: uses('CakeEmail','Network / Email');

$ email = new CakeEmail();

$ email-> from('cameron@driz.co.uk');

$ email-> to('cameron@driz.co.uk');

$ email-> subject('Test Email from Cron');

$ result = $ email-> send('Hello from Cron');

}

}

?>

我想说的是每晚午夜运行这个代码。



我接下来该怎么办?作为下一部分真的困惑我!我已阅读了本书: http:// book .cakephp.org / 2.0 / en / console-and-shells / cron-jobs.html ,我应该在终端运行一些代码,使它在某一时间等等。我可以设置这些使用我的托管提供商很容易似乎。



但我很困惑的控制台目录。应该在什么文件夹中: https://github.com/cakephp/ cbephp / tree / master / app / Console

  / Console / Command 
/任务
/控制台/模板

还注意到许多文件是.php我的Shell文件也是.php),但是根据我为Cron作业读取的文档,执行的文件应该是.sh?



任何人都可以打开更多的光这是什么?



例如:会假定这是不正确的: 0 0 * * * cd /domains/driz.co.uk/html/App&&蛋糕/控制台MyShell sendEmail



感谢

解决方案

不。没有办法只是在PHP。但是这并不重要,因为cron很容易设置。



在你链接到的文章中,你还需要设置一个cron - 区别只是你设置一个单一的cron,运行所有其他crons - 而不是每个作业设置一个cron。因此,无论哪种方式,您都必须学会创建一个cron。



这些说明取决于您的服务器操作系统以及您使用的主机。有些主机可以通过GUI界面(如cPanel或其他东西)设置cron作业,而无需触摸终端。



通常很容易找到说明在线了解如何使用主机或服务器操作系统设置cron作业,但如果遇到问题,请使用您的主机名称以及您的服务器操作系统和版本更新您的问题。



也---------------------------------



通常在cron作业中,你将运行一个shell脚本(.sh)。但不要担心这种情况;


$ b





如果你做了很多shell程序,你可能想抽象常用的代码到 / Console / Command / Task 文件夹中。有关详情,请参阅此处。这可能不需要在您的情况下。



/控制台/命令/模板



如果我只有几个cron作业要运行,那么我创建只是一个名为CronJobsShell.php的文件,并将它们全部放在那里。



真的,你应该阅读Cake的 shell上的文档。它会给你一个很好的照片,它如何一起挂在一起。


I have attempted to create a cron job in my CakePHP 2.x application. But all of the resources I have read online seem to be either doing it completely different to one another with little consistency or explain it in very complex terminology.

Basically I have created the following file MyShell.php in /app/Console/Command

<?php 

class MyShell extends Shell {

    public function sendEmail() {

        App::uses('CakeEmail', 'Network/Email');

        $email = new CakeEmail();

        $email->from('cameron@driz.co.uk');

        $email->to('cameron@driz.co.uk');

        $email->subject('Test Email from Cron');

        $result = $email->send('Hello from Cron');

    }

}

?>

And I want to say run this code at midnight every night.

What do I do next? As the next part really confuses me! I have read on the Book at: http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html that I should run some code in the terminal to make it do it at a certain time etc. And I can set these up using my hosting provider rather easily it seems.

But I'm rather confused about the Console directory. What should go in what folder in here: https://github.com/cakephp/cakephp/tree/master/app/Console

/Console/Command
/Console/Command/Tasks
/Console/Templates

Also noticed that many of the files are .php (e.g. my Shell file is also .php), but according to documentation I've read for Cron jobs, the executed files should be .sh?

Can anyone shed more light on this?

And what would the code be to call that command?

e.g. would presume this is incorrect: 0 0 * * * cd /domains/driz.co.uk/html/App && cake/Console MyShell sendEmail

Thanks

解决方案

No. There is no way to do it just in PHP. But that doesn't matter, because crons are easy to set up.

In that article you linked to, you still have to set up a cron - the difference is just that you set up a single cron, that runs all your other crons - as opposed to setting up one cron per job. So, either way, you have to learn to create a cron.

The instructions depend on your server's operating system and also what host you're with. Some hosts will have a way to set up cron jobs through a GUI interface like cPanel or something, without you having to touch the terminal.

It's usually pretty easy to find instructions online for how to set up cron jobs with your host or server OS, but if you're having trouble, update your question with your host's name, and your server OS and version.

Also ---------------------------------

Often in cron jobs you'll be running a shell script (.sh). But don't worry about that for this case; your's will end in .php.

Re: the directory structure:

/Console/Command is where your new file should go.

If you're doing a lot of shell stuff, you may want to abstract common code out into the /Console/Command/Task folder. Read more about that here. This probably won't be needed in your case.

/Console/Command/Templates is where you can put custom templates for the Cake bake console - don't worry about that for now.

If I've only got a couple of cron jobs to run, then I create just one file called CronJobsShell.php, and put them all in there.

Really, you should read Cake's documentation on shells from start to end. It will give you a nice picture of how it all hangs together.

这篇关于在CakePHP 2.x中创建Cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆