Laravel "No scheduled commands are ready to run."

Laravel "No scheduled commands are ready to run."

我设置了以下 Laravel 命令:

protected function schedule(Schedule $schedule) {
        $schedule->command('command:daily-reset')->daily();
        $schedule->command('command:monthly-reset')->monthly();
}

然后,在我的服务器上,我将 cron 作业设置为每天 运行 一次(在 00:00)。

0 0 * * * php /home/privates/public_html/staging/current/artisan schedule:run

我的 cron 作业每晚都 运行 成功,但日志只是说:"No scheduled commands are ready to run."

我做错了什么?我希望我的 daily 命令每晚 运行。

谢谢!

您是否手动尝试过 运行 命令?

运行 php artisan 并查看您的命令是否已注册。

如果您已经注册了命令,您应该会在可用的 artisan 命令列表下看到 command:daily-resetcommand:monthly-reset

如果您没有看到它们,请继续并通过将其添加到 app/Console/Kernel.php.

中可用的 commands 属性 来注册您的命令
protected $commands = [
    'App\Console\Commands\YourFirstCommand',
    'App\Console\Commands\YourSecondCommand'
];

将 crontab 条目更改为

* * * * * php /home/privates/public_html/staging/current/artisan schedule:run

我认为我的博客可以帮助您回答您的问题。请看下面或link:Laravel Crontab 在许多项目中,您需要使用 crontab(cron 作业)来执行一些任务,例如发送电子邮件或删除 DB 中的垃圾记录。使用 Laravel 项目,您可以更轻松地做到这一点。

在Laravel 4中创建命令:

<?php
 
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
 
class FirstCommand extends Command {
 
        /**
         * The console command name.
         *
         * @var string
         */
        protected $name = 'user:active';
 
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Command description.';
 
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
                parent::__construct();
        }
 
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function fire()
        {
                echo "User Actived";
        }
        /**
         * Get the console command arguments.
         *
         * @return array
         */
        protected function getArguments()
        {
                return array(
                );
        }
 
        /**
         * Get the console command options.
         *
         * @return array
         */
        protected function getOptions()
        {
                return array(
                        array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
                );
        }
 
}

下一步,您需要使用 Laravel CLI 注册命令。很简单,你打开 app/start/artisan.php 文件,并添加一行如下:

Artisan::add(new FirstCommand);

您已完成创建 Laravel 命令。要进行测试,您可以使用以下命令:

$ php artisan user:active

用户活跃 上面的输出表示你成功注册了一个命令。

最后,将您的命令放入 crontab:

crontab -e

添加行(运行 命令每 2 分钟):

*/2 * * * * php path_to_laravel_project/artisan user:active

就是这样。感谢您花时间阅读本文。

注意:这不是这个问题的答案,而是为任何使用 php artisan schedule:run 手动调试的人提供的线索。希望它可以让某人省去几分钟的头痛。

检查计划任务是否可以立即运行。您可以为此使用 exec 方法。

<?php
...

protected function schedule (Schedule $schedule) {
    $schedule -> exec("php artisan your:command");
}

原因是,您可能将任务安排在某个时间运行,如果该时间尚未到期,它将输出:"No scheduled commands are ready to run."

Laravel 计划命令基于您在 app/config/app.php 文件 (laravel 5.1) 中配置的时区:

/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'America/Bogota',

因此,如果您创建一个命令并将其作为计划任务注册到 运行:

$schedule->command('command:daily-reset')->daily();

它将在指定时区的 00:00 每天 运行(在本例中为 America/Bogota)

如果您为 运行 任务指定时间,同样适用:

$schedule->command('command:daily-reset')->daily()->at('02:30');

这将在当地时间 America/Bogota 上午 02:30 运行。

我意识到我的问题是以下链接方法:

->withoutOverlapping() 

删除该方法后,我的命令开始 运行 并被守护进程找到。

我认为该方法可能存在错误,但我现在的项目可能会有一些重叠,所以很酷。

当你 运行

php artisan schedule:run

在存储项目的服务器中,您可以看到所有命令 运行 输出,如下所示:

"Running scheduled command: '/usr/local/bin/php' 'artisan' cache:update > '/dev/null' 2>&1 &"

但前提是当前时间与预定命令的时间完全一致。否则你会看到这个输出:

"No scheduled commands are ready to run."

例如,如果您每五分钟安排一次命令,运行 在 09:07 点安排命令,您将看到没有安排的命令,但是如果您 运行 它在 09:10 你会看到你的命令 运行ning。

通过这种方式,您可以将命令安排为每 5 分钟 运行,仅用于调试目的:

$schedule->command('command:daily-reset')->everyFiveMinutes();

然后在运行ning的时候观察有没有错误,最后修复。我的问题是我还没有安装 GuzzleHttp(可耻),所以修复只是 运行 在终端中设置:

composer require guzzlehttp/guzzle

据我所知,上面没有列出这个问题的完整答案。假设我们的日程安排如下:

protected function schedule(Schedule $schedule)
{
    $schedule
        -> command('cbh:dummyCommand')
        -> everyFiveMinutes()
        -> appendOutputTo ('/my/logs/laravel_output.log');
}

我发现此代码不会将您的作业设置为每 5 分钟 运行。如果 运行 不到 5 分钟前,它也不会再次阻止命令 运行ning。

更好的思考方式是这段代码设置命名命令"to be runnable every time the minute-figure of the current time is 0 or 5"。换句话说,如果我 运行 命令行参数:php artisan schedule:run11:04,那么响应是:

# No scheduled commands are ready to run.

但是如果我 运行 在 11:0011:05 处执行相同的命令,那么我们会得到:

# Running scheduled command: php artisan cbh:dummyCommand >> /my/logs/laravel_output.log 2>&1

最后我的日志文件中有输出。

当我的 everyFiveMinutes() 计划每 10 分钟在我的文件中创建一个日志时,我发现了上述情况,因为我的任务计划程序每 2 分钟 运行ning 一次。

但是,鉴于 daily() 计划 (0 0 * * *) 与您的 cron-job 计划一致,这并不能完全解决您的问题。我唯一可以想象的是,正如@Octavio Herrera 所建议的那样,您的时区存在某种错位。但是,如果不进一步了解您的环境,就很难说。

在 Windows,我通过将计划任务设置为 运行 每分钟 解决了这个问题(尽管我只想触发一次命令每天),否则我总是收到 No scheduled commands are ready to run. 消息。

我遇到了同样的问题。每个命令都正确注册,但我总是收到“没有计划的命令准备好运行。”消息。问题是网站在 "maintenance mode" (php artisan down 命令),而我们正在进行更新和测试。

因为 4 年后(2019 年)我仍然 运行 关注这个问题并且不同的解决方法对我有用,我认为值得提示为我解决的简单步骤,即:使用更短的先间隔

这似乎只是唤醒它以在某些方面处理更长的间隔。我有 everyFiveMinutes() 并且在将近 2 个小时的时间里它得到了 No scheduled commands are ready to run 响应。我只是将其更改为 everyMinute() 并正确启动 运行。我一直看了大约 10 分钟左右,然后将其改回 everyFiveMinutes(),一切都很顺利。

这个问题我已经坚持了No scheduled commands are ready to run.一个小时,但很容易就解决了:

问题与文件夹 存储.

的权限有关

所以,我设置了 chmod -R 777 storage/*(我不确定这是不是优雅的方式)。

之后 cron 开始正常工作。

要 运行 本地服务器 上的 Cron 命令,请执行以下步骤:

  1. 我知道你已经在app/console/Kernel中提到了命令。php
  2. 现在打开命令行,输入“crontab -e”
  3. 编辑该文件并提及以下代码(不带引号)以在后台保留 运行ning PHP artisan schedule:run

"* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1"

  1. 在命令行输入“crontab -l”,会列出运行ning crons

完成!!

现在,等待 cron 处理您的命令。干杯!!

参考- https://laravel.com/docs/7.x/scheduling#introduction

无论出于何种原因,cron 无法识别您任务的命名版本。

所以在你的日程中而不是写作

$schedule->command('command:task');

你应该使用class的路径,比如

$schedule->command(\App\Console\Commands\TASK::class)

...Laravel Forge 上的调度程序也是如此!

我已经尝试了所有方法,但最终我找到了解决这个问题的方法。在命令中添加时间戳。下面是这个例子。

     $schedule->call(function(){
         print("HELLO");
     })->dailyAt('21:51')->timezone('Asia/Kolkata');

$schedule->command('tenam:before')
        ->dailyAt('22:28')->timezone('Asia/Kolkata');