Laravel-4 定时任务

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

Laravel-4 cron jobs

phplaravellaravel-4laravel-3

提问by John

I need to learn how to work with cron jobs in Laravel..As I can see the documentation does not specify this part.I have found a tutorial but it is about Laravel-3. Can you give me some advice on how to schedule a cron job running once a day..?Is there any tutorial about that issue?

我需要学习如何在 Laravel 中使用 cron 作业。我可以看到文档没有指定这部分。我找到了一个教程,但它是关于 Laravel-3 的。你能给我一些关于如何安排每天运行一次的 cron 作业的建议吗?有没有关于这个问题的教程?

My code so far is the following :

到目前为止,我的代码如下:

JobDaemon.php :

JobDaemon.php :

<?php

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

class JobDaemon extends Command {

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'job-daemon';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Get all recent jobs once a day.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        $this->info('fired');
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return array(
            //array('example', InputArgument::REQUIRED, 'An example argument.'),
        );
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return array(
            //array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
        );
    }

}

I used the following command to set it up

我使用以下命令进行设置

php artisan command:make JobDaemon

And my artisan file is the following:

我的工匠文件如下:

<?php

Artisan::add(new JobDaemon);

I get the following from my console...

我从我的控制台得到以下...

johnnemo@johnnemo:/opt/lampp/htdocs/e-support-uop$ tail -f /var/log/syslog | grep -i cron
Jan  1 18:31:09 johnnemo crontab[4484]: (johnnemo) REPLACE (johnnemo)
Jan  1 18:31:09 johnnemo crontab[4484]: (johnnemo) END EDIT (johnnemo)
Jan  1 18:35:01 johnnemo CRON[5054]: (johnnemo) CMD (php /opt/lampp/htdocs/e-support-uop/artisan job-daemon)
Jan  1 18:35:02 johnnemo CRON[5053]: (CRON) info (No MTA installed, discarding output)
Jan  1 18:39:01 johnnemo CRON[5064]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -x /usr/lib/php5/sessionclean ] && [ -d /var/lib/php5 ] && /usr/lib/php5/sessionclean /var/lib/php5 $(/usr/lib/php5/maxlifetime))
Jan  1 18:40:01 johnnemo CRON[5076]: (johnnemo) CMD (php /opt/lampp/htdocs/e-support-uop/artisan job-daemon)
Jan  1 18:40:01 johnnemo CRON[5075]: (CRON) info (No MTA installed, discarding output)

回答by Antonio Carlos Ribeiro

First you need to make sure your new command is up, so if you run

首先,您需要确保新命令已启动,因此如果您运行

php artisan list

'job-daemon' must be in the list of commands

'job-daemon' 必须在命令列表中

Then you test it:

然后你测试一下:

php artisan job-daemon

Does it work? Cool, now you can set an editor of your own:

它有效吗?很酷,现在您可以设置自己的编辑器:

export EDITOR=nano

Open the crontab with it:

用它打开 crontab:

[sudo] crontab -e

Execute type to the the correct path for your php:

执行类型到您的 php 的正确路径:

type php

And you should get something like

你应该得到类似的东西

php is hashed (/opt/lampp/bin/php)

So your php executable is at

所以你的 php 可执行文件在

/opt/lampp/bin/php

This will open and editor with the current cron jobs, sudo is optional to open the rootcrontab, just add a line with yours:

这将打开并编辑当前的 cron 作业,sudo 是打开crontab 的可选选项,只需添加一行:

25 10 * * * /opt/lampp/bin/php /whatever/directory/your/site/is/artisan job-daemon

This will run your command everyday at 10:25AM.

这将在每天上午 10:25 运行您的命令。

To execute it every 5 minutes you do

每 5 分钟执行一次

*/5 * * * * /opt/lampp/bin/php /whatever/directory/your/site/is/artisan job-daemon

Then you tailthe syslog to see it running:

然后您tail查看系统日志以查看它正在运行:

tail -f /var/log/syslog | grep -i cron

And you should see something like

你应该看到类似的东西

Jan  1 10:25:01 server CRON[19451]: (root) CMD (php /var/www/<siteName>/artisan job-daemon)

In your command you cann't really print things on the screen, you won't see them printing, so to test you have to, for instance, save something to a file:

在你的命令中,你不能真正在屏幕上打印东西,你不会看到它们打印,所以为了测试你必须,例如,将一些东西保存到文件中:

public function fire()
{
    File::append('/tmp/laravel.txt', "fired\n");
    Log::info('fired');
}

And then

进而

tail -f /tmp/laravel.txt

To see the results in realtime.

实时查看结果。

回答by Karl Schneider

I had a similar question to the OP, Antonio's answer got me most of the way there, but not 100%. I was attempting to schedule the job from the cPanel CRON Jobs page, and encountered either 404 errors or no error, but no success either. For me, the key was the following:

我有一个与 OP 类似的问题,Antonio 的回答让我找到了大部分方法,但不是 100%。我试图从 cPanel CRON 作业页面安排作业,遇到 404 错误或没有错误,但也没有成功。对我来说,关键在于:

  1. Putty into my server, run the command:

    php artisan list

  2. Ensure my command was listed

  3. Run the next command:

    type php

  4. For me, the output was "php is hashed (/usr/local/bin/php)"

  5. When creating the CRON command, I had to use the qualified path. In addition, since artisan isn't globally available, you also have to use a qualified path to where it's available. For me, this was the laravel folder of my site. The last gotcha was that I had to prefix the actual artisan command with "command:"

  1. Putty 进入我的服务器,运行命令:

    php工匠列表

  2. 确保我的命令被列出

  3. 运行下一个命令:

    输入php

  4. 对我来说,输出是“php is hashed (/usr/local/bin/php)”

  5. 创建 CRON 命令时,我必须使用限定路径。此外,由于 artisan 不是全局可用的,因此您还必须使用合格的路径来访问它的可用位置。对我来说,这是我网站的 laravel 文件夹。最后一个问题是我必须在实际的工匠命令前加上“命令:”

In the end, I was able to schedule a laravel command via cPanel's CRON Jobs page using the following command:

最后,我能够使用以下命令通过 cPanel 的 CRON 作业页面安排 laravel 命令:

/usr/local/bin/php /home/sitename/public_html/laravel/artisan command:TotalMadnessUpdateResultsCommand

Another common gotcha is not adding the following to your artisan.php file:

另一个常见的问题是没有将以下内容添加到您的 artisan.php 文件中:

Artisan::add(new TotalMadnessUpdateResultsCommand);