使用 Laravel 4 的 Cron 作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14520955/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:22:28  来源:igfitidea点击:

Cron Job with Laravel 4

laravellaravel-4croncommand

提问by TheSteed

I'm trying to find out how to set up a cron job in Laravel 4, and the command I would need to run in artisan for it.

我试图找出如何在 Laravel 4 中设置 cron 作业,以及我需要在 artisan 中运行的命令。

In Laravel 3, there were Tasksbut these don't seem to be there anymore and there is no documentation as to how to do it...

在 Laravel 3 中,有Tasks但这些似乎不再存在,并且没有关于如何做的文档......

采纳答案by Dan Matthews

Tasks have been replaced with commands, which are the same thing in Laravel 4, but integrated with Symfony's console component, and even more powerful than before.

Tasks 已经被command替换了,这在 Laravel 4 中是一样的东西,但是集成了 Symfony 的控制台组件,甚至比以前更强大。

回答by tomloprod

Below I detail a tutorial for using commandsin Laravel 4with cron. I have divided into four steps to make it easier to follow.

下面我详细使用教程commandsLaravel 4:用cron。为了更容易理解,我分为四个步骤。



STEP #1: Create a command in Laravel 4:

第 1 步:在 Laravel 4 中创建一个命令:

php artisan command:make RefreshStats

With command above, Laravel will create a file named RefreshStats.phpin directory app/commands/

使用上面的命令,Laravel 将创建一个RefreshStats.php在目录中命名的文件 app/commands/


RefreshStats.phpit's a file like this:


RefreshStats.php它是一个这样的文件:

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class RefreshStats extends Command {

        protected $name = 'command:name';
        protected $description = 'Command description.';

        public function __construct() {
                parent::__construct();
        }

        public function fire(){

        }

        protected function getArguments() {
            return array(
                array('example', InputArgument::REQUIRED, 'An example argument.'),
            );
        }

        protected function getOptions() {
            return array(
                array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
            );
        }

}





STEP #2: Simple "configuration" of RefreshStats file:

第 2步:RefreshStats 文件的简单“配置”:

You should change this line:

你应该改变这一行:

protected $name = 'command:name';

to something like this:

像这样:

protected $name = 'refresh:stats';

If you don't need arguments (same for options), change this line:

如果您不需要参数(与 options 相同),请更改此行:

protected function getArguments() {
      return array(
          array('example', InputArgument::REQUIRED, 'An example argument.'),
      );
}

to:

到:

protected function getArguments() {
      return array();
}

And now pay attentionto the firefunction. The command will execute source code that is wrote in that function. Example:

而现在注重fire功能。该命令将执行在该函数中编写的源代码。例子:

public function fire(){
    echo "Hello world";    
}





STEP #3: Register the command:

第 3 步:注册命令:

You need to register the command. So open app/start/artisan.phpfile, and add one line as below:

您需要注册该命令。所以打开app/start/artisan.php文件,并添加如下一行:

Artisan::add(new RefreshStats);





STEP #4: Create CRONscheduled task:

第 4:创建CRON计划任务:

Finally, you can add a scheduled task as follow:

最后,您可以添加计划任务如下:

crontab -e

And add a line (run command every 30 minutes) like below:

并添加一行(每 30 分钟运行一次命令),如下所示:

*/30 * * * * php path_laravel_project/artisan refresh:stats





And all will work automagically!

并且一切都会自动运行

回答by Marc

Alternative, if you don't like commands there is an unofficial Laravel 4 cron package: https://github.com/liebig/cron

或者,如果你不喜欢命令,有一个非官方的 Laravel 4 cron 包:https: //github.com/liebig/cron

回答by Hafiz Siddiq

OK so I found thisuseful for setting up crons in laravel 4.2.

好的,所以我发现对于在 laravel 4.2 中设置 cron 很有用。