php Laravel 5 计划不起作用

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

Laravel 5 schedule not working

phplaravelcronlaravel-5

提问by lighter

My laravel version is 5.0.28, I build on cloud9, and I added this command to my cron:

我的 Laravel 版本是 5.0.28,我在 cloud9 上构建,并将此命令添加到我的 cron 中:

#!/bin/bash
PATH=/usr/bin
* * * * * php /home/ubuntu/workspace/app/artisan scheduled:run 1>> /dev/null 2>&1

I added this code on my Kernel.php. I referenced this site: https://laravel-news.com/2014/11/laravel-5-scheduler/

我在我的Kernel.php. 我参考了这个网站:https: //laravel-news.com/2014/11/laravel-5-scheduler/

<?php namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Http\Controllers\ApiController;

class Kernel extends ConsoleKernel {

    protected $commands = [
        'App\Console\Commands\Inspire',
    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->call('ApiController@test_job')->hourly();
    }
}

I waited and it still didn't work, so I tried to use the command php artisan schedule:run, and I got: No scheduled commands are ready to run.

我等了一会儿还是不行,所以我尝试使用命令php artisan schedule:run,我得到:No scheduled commands are ready to run.

I searched and found this answer: Laravel 5 "Class does not exist" when using the scheduler

我搜索并找到了这个答案:Laravel 5 "Class does not exist" when using the scheduler

So I modified my code. Also, this code had no specified time, so I modified my cronto specify a time, but it still doesn't work. I have no more ideas. please help. Thanks.

所以我修改了我的代码。另外,这段代码没有指定时间,所以我修改了我cron的指定时间,但它仍然不起作用。我没有更多的想法。请帮忙。谢谢。

code

代码

$schedule->call(join('@', [ApiController::class, 'test_job']));

cron

定时任务

0 0,3,6,9,12,15,18,21 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
30 1,4,7,10,13,16,19,22 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1

采纳答案by Limon Monte

Laravel schedulerworks with commands, not with controller methods:

Laravel 调度程序与命令一起工作,而不是与控制器方法一起工作:

  1. create command:
  1. 创建命令:
php artisan make:command PurchasePodcast
  1. edit command:
  1. 编辑命令:
namespace App\Console\Commands;

use Illuminate\Console\Command;

class PurchasePodcast extends Command
{
    protected $name = 'purchase:podcast';

    public function fire()
    {
        // do stuff here
    }
}
  1. add command to Console\Kernel.php:
  1. 将命令添加到Console\Kernel.php
protected $commands = [
    'App\Console\Commands\PurchasePodcast',
];
  1. use command in scheduler:
  1. 在调度程序中使用命令:
$schedule->command('purchase:podcast')->hourly();

回答by Grigoreas P.

first to Test if cron is running in your server or localhost type:

首先测试 cron 是否在您的服务器或本地主机类型中运行:

> sudo service cron status

if not installed:

如果没有安装:

> sudo apt-get install cron

to enable laravel's scheduler:

启用 Laravel 的调度程序:

> crontab -e

and you can select an editor if not vim opens directly. Be sure to enter there this line at the bottom:

如果没有vim直接打开,您可以选择一个编辑器。请务必在底部输入此行:

* * * * * php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1

to Test if you have setup inside laravel the scheduler right, run this from your projects folder:

要测试您是否在 Laravel 中正确设置了调度程序,请从您的项目文件夹中运行它:

>php artisan schedule:run

this should execute the tasks and tell you what is doing.

这应该执行任务并告诉您正在做什么。

回答by lydiacx

There are several aspects to find the cause: First, check whether the 'timezone' in config/app.php is set properly. Laravel will reset the timezone even though you already configured it in php.ini. Secondly, check that crontab is working as expected. When you get the message "No schedule to be ready", it means your crontab is running and can detect the php and artisan command.

查找原因有几个方面: 首先,检查config/app.php中的'timezone'是否设置正确。即使您已经在 php.ini 中配置了时区,Laravel 也会重置时区。其次,检查 crontab 是否按预期工作。当您收到消息“No schedule to be ready”时,表示您的 crontab 正在运行并且可以检测到 php 和 artisan 命令。

回答by Eugen Zaharia

In order to complete @limonte's answer the create Console Command is the following:

为了完成@limonte 的回答,创建控制台命令如下:

php artisan make:console CampaignsCollect --command=campaigns:collect

Reference here: link

参考这里:链接