Laravel-任务计划 [英] Laravel - Task Scheduling
问题描述
我在本地WAMP服务器上使用Laravel 5.4.我想执行任务计划,但不确定我是否真的了解它的工作原理.
I'm using Laravel 5.4 on a local WAMP Server. I wanted to perform a Task Scheduling but I'm not sure if I really understood how this works.
我创建了一个命令cronEmail,并在handle()函数中添加了获取电子邮件的代码.在Kernel.php中,我添加了以下内容:
I created a command cronEmail and in the handle() function added code where I would get an Email. In Kernel.php I added this:
protected $commands = [
'App\Console\Commands\cronEmail'
];
...
protected function schedule(Schedule $schedule)
{
$schedule->command('send:email')
->everyMinute();
}
所以基本上我想每分钟收到一封电子邮件.但是我该如何开始呢?我尝试输入:
So basically I want to get an email every minute. But how do I start this? I tried entering:
php artisan schedule:run >>/dev/null 2>& 1
php artisan schedule:run >> /dev/null 2>&1
甚至
php C:\ wamp64 \ www \ seo-parser \ artisan schedule:run >>/dev/null 2>& 1
php C:\wamp64\www\seo-parser\artisan schedule:run >> /dev/null 2>&1
在我的cmd上,但我总是得到:系统找不到指定的路径.
on my cmd but I always get: The system cannot find the path specified.
如果我输入php artisan schedule:run,它实际上会发送一封电子邮件,但只会发送一次.
If i enter php artisan schedule:run it will actually send an email but only once.
我理解整个概念错了吗?如何正确执行此操作?
Did I understand the whole concept wrong? How do I do this properly?
谢谢你,帕特里克
推荐答案
如 Laravel官方文档中所述,您需要将以下行添加到crontab中.
As stated in the official Laravel documentation you need to add the following line to your crontab.
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
您可以通过在控制台中键入"crontab -e"来执行此操作.粘贴以上行,并将"/path-to-your-project"替换为项目的路径.
You do this by typing "crontab -e" in the console. Paste the above line and replace the "/path-to-your-project" with the path to your project.
这将导致cronjob每分钟调用一次"php artisan schedule:run"命令.
This results in a cronjob wich calls the "php artisan schedule:run" command every minute.
这需要您运行Linux.如果您在运行Windows时需要crontab的替代产品,可以先在此处阅读.
This requires you to run Linux though. If you need an alternative to crontab when running Windows you can start by reading here.
这篇关于Laravel-任务计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!