在特定时间运行 Laravel 任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17128857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running Laravel Task at specific time
提问by ecampver
I've seen some similar questions at SO but none of then have answered me. I had never heard of CRON before and I'm new to Laravel. What I need is to run a Task once a week to perform some actions on my database (MySql), say every sunday at 12:00 am. How could I achieve this goal?
我在 SO 看到了一些类似的问题,但没有一个回答我。我以前从未听说过 CRON,而且我是 Laravel 的新手。我需要的是每周运行一次任务以对我的数据库 (MySql) 执行一些操作,例如每个星期日的上午 12:00。我怎样才能实现这个目标?
回答by Antonio Carlos Ribeiro
If you can use cron, you just have to execute
如果你可以使用cron,你只需要执行
crontab -e
Or, if you need to run as root:
或者,如果您需要以 root 身份运行:
sudo crontab -e
This will open a text editor so you can modify your crontab and there you'll have one line for each scheduled command, as this one:
这将打开一个文本编辑器,以便您可以修改您的 crontab,并且每个预定命令都有一行,如下所示:
1 0 * * * php /var/www/myBlog/artisan task:run
The command in this like will be executed at the first minute of every day (0h01 or 12h01am).
这样的命令将在每天的第一分钟(0h01 或 12h01am)执行。
Here is the explanation of it all:
以下是对这一切的解释:
* * * * * <command to execute>
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names)
│ │ │ └────────── month (1 - 12)
│ │ └─────────────── day of month (1 - 31)
│ └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)
So, in your case, you'll create a line like this:
因此,在您的情况下,您将创建如下一行:
0 12 * * 0 <command to execute>
But how do you do that for a task in Laravel? There are many ways, one of them is in my first example: create an artisan command (task:run) and then just run artisan, or you can just create a route in your app to that will call your task every time it is hit:
但是你如何为 Laravel 中的任务做到这一点?有很多方法,其中一个在我的第一个示例中:创建一个 artisan 命令 (task:run) 然后只运行 artisan,或者您可以在您的应用程序中创建一个路由,该路由每次被点击时都会调用您的任务:
Route::get('/task/run',array('uses'=>'TaskController@run'));
And then you just have to add it your crontab, but you'll need something to hit your url, like wget or curl:
然后你只需要将它添加到你的 crontab 中,但是你需要一些东西来访问你的 url,比如 wget 或 curl:
0 12 * * 0 curl http://mysite.com/run/task
回答by Woodrow Norris
Since Laravel 5, the only entry you need to put into crontab after executing crontab -e
is
从 Laravel 5 开始,执行后唯一需要放入 crontab 的条目crontab -e
是
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
Do remember to change /path/to/artisan
part to your project specific path.
请记住将/path/to/artisan
部分更改为您的项目特定路径。
And then you can define your scheduled tasks and running frequency in Laravel's App\Console\Kernel
class. See Laravel documentation for more information: Task Schedule
然后你可以在 Laravel 的App\Console\Kernel
类中定义你的计划任务和运行频率。有关更多信息,请参阅 Laravel 文档:任务计划
回答by msurguy
You can create and run your laravel tasks from the command line just like any other Artisan command. So if you are on windows you can just run the command manually to see if it works or not.
您可以像任何其他 Artisan 命令一样从命令行创建和运行 Laravel 任务。因此,如果您在 Windows 上,则可以手动运行该命令以查看它是否有效。
Then when you are on production server (better of course if it is Unix based) then you can follow Antonio's direction to create the cron job and add it to the cron tab. Keep in mind you will need to know the complete paths for the PHP execution.
然后,当您在生产服务器上时(当然如果它是基于 Unix 的更好),那么您可以按照 Antonio 的指示创建 cron 作业并将其添加到 cron 选项卡中。请记住,您需要知道 PHP 执行的完整路径。
My tutorial explains this all in detail : http://maxoffsky.com/code-blog/practical-laravel-using-cron-jobs-in-laravel/
我的教程详细解释了这一切:http: //maxoffsky.com/code-blog/practical-laravel-using-cron-jobs-in-laravel/
Hope you find the answer that you are looking for.
希望你能找到你正在寻找的答案。