简体   繁体   English

Laravel “没有计划的命令可以运行。”

[英]Laravel "No scheduled commands are ready to run."

I've set up the following Laravel commands on the App\Console\Kernel :我在App\Console\Kernel上设置了以下 Laravel 命令:

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

Then, on my server, I've set up a cron job to run once per day (at 00:00).然后,在我的服务器上,我设置了一个每天运行一次的 cron 作业(在 00:00)。

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

My cron job is running successfully each night, but the logs simply say: "No scheduled commands are ready to run."我的 cron 作业每晚都成功运行,但日志只是说:“没有计划的命令准备好运行。”

What am I doing wrong?我究竟做错了什么? I would expect my daily command to run each night.我希望我的daily命令每晚都能运行。

Thanks!谢谢!

When you run当你跑

php artisan schedule:run

in the server, where your project is stored, you could see all of your commands running with output, looking like this:在存储您的项目的服务器中,您可以看到所有运行的命令和输出,如下所示:

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

but only if the current time is the exact one, for which the command is scheduled.但前提是当前时间是为该命令安排的确切时间。 Otherwise you are going to see this output:否则你会看到这个输出:

"No scheduled commands are ready to run."

For example, if you schedule the command for every five minutes and run the command in 09:07 o'clock you will see that there are no scheduled commands, but if you run it in 09:10 you will see your command running.例如,如果您每五分钟安排一次命令并在 09:07 点运行该命令,您将看到没有安排好的命令,但如果您在 09:10 运行它,您将看到您的命令正在运行。

In this way you can just schedule your command to run every 5 min just for debugging purposes:通过这种方式,您可以将命令安排为每 5 分钟运行一次,仅用于调试目的:

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

then observe if there is any error while running and eventually fix it.然后观察运行时是否有任何错误并最终修复它。 By me the problem was that I haven't installed GuzzleHttp (shame), so the fix was just running this in the terminal:我的问题是我没有安装 GuzzleHttp(耻辱),所以修复只是在终端中运行它:

composer require guzzlehttp/guzzle

I realized that the problem for me was the below chained method:我意识到我的问题是下面的链接方法:

->withoutOverlapping() 

Once I removed that method, my commands started running and being found by the daemon process.删除该方法后,我的命令开始运行并被守护进程找到。

I think there might be a bug with the method, but my project for now can take a bit overlapping so it's cool.我认为该方法可能存在错误,但我现在的项目可能会有些重叠,因此很酷。

Did you try running command manually?您是否尝试过手动运行命令?

Run php artisan and see if your commands have registered.运行php artisan并查看您的命令是否已注册。

If you have registered your commands you should see command:daily-reset and command:monthly-reset under the list of available artisan commands.如果你已经注册了你的命令,你应该在可用的 artisan 命令列表下看到command:daily-resetcommand:monthly-reset

If you don't see them there go ahead and register your commands by adding it to commands property available in app/Console/Kernel.php .如果您没有看到它们,请继续并通过将其添加到app/Console/Kernel.php可用的commands属性来注册您的命令。

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

Change crontab entry to将 crontab 条目更改为

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

The Laravel scheduled commands are based in the timezone that you have configured in your app/config/app.php file (laravel 5.1): 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',

So if you create a command and register it to run as a scheduled task with:因此,如果您创建一个命令并将其注册为作为计划任务运行:

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

it will run every day at 00:00 OF THE TIMEZONE SPECIFIED (in this case America/Bogota)它将每天在指定时区的 00:00 运行(在这种情况下是美国/波哥大)

The same thing applies if you specify a time to run the task:如果您指定运行任务的时间,则同样适用:

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

This will run at 02:30 am in America/Bogota local time.这将在美国/波哥大当地时间凌晨 02:30 运行。

NB: This is not answer for this question, but a clue for anyone debugging with php artisan schedule:run manually.注意:这不是这个问题的答案,而是任何使用php artisan schedule:run调试的人的线索。 Hope it saves someone a few minutes of headache.希望它可以为某人节省几分钟的头痛。

Check if the scheduled task can run immediately.检查计划任务是否可以立即运行。 You can use the exec method for that.您可以为此使用exec方法。

<?php
...

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

The reason for this is that, you might be scheduling the task to run at a certain time and if that time isn't due yet, it will output: "No scheduled commands are ready to run."这样做的原因是,您可能将任务安排在某个时间运行,如果该时间尚未到期,它将输出:“没有准备好运行的计划命令。”

The full answer to this question is not listed above as far as I can see.据我所知,上面没有列出这个问题的完整答案。 Let's assume that our schedule is as follows:假设我们的日程安排如下:

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

What I've discovered is that this code doesn't set your job to run every 5 minutes.我发现此代码不会将您的工作设置为每 5 分钟运行一次。 Nor does it prevent the command running again if it was run less than 5-minutes ago.如果命令在不到 5 分钟前运行,它也不会阻止命令再次运行。

A better way to think about it is that this code sets the named command "to be runnable every time the minute-figure of the current time is 0 or 5 " .更好的思考方式是,这段代码将命名命令设置为“每次当前时间的分钟数字为05时都可运行” In other words, if I run the command-line argument: php artisan schedule:run at 11:04 , then the response is:换句话说,如果我运行命令行参数: php artisan schedule:run at 11:04 ,那么响应是:

# No scheduled commands are ready to run.

But if I run the same command at 11:00 or 11:05 , then we get:但是如果我在11:0011:05运行相同的命令,那么我们得到:

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

And I end up with output in my log-file.我最终在我的日志文件中输出。

I discovered the above when my everyFiveMinutes() schedule was creating a log in my file every 10 minutes based on the fact that my task-scheduler was running every 2 minutes.当我的everyFiveMinutes()计划每 10 分钟在我的文件中创建一个日志时,我发现了上述情况,因为我的任务计划程序每 2 分钟运行一次。

However, this doesn't quite address your issue, given that the daily() schedule ( 0 0 * * * ) aligns with your cron-job schedule.但是,这并不能完全解决您的问题,因为daily()计划 ( 0 0 * * * ) 与您的 cron-job 计划一致。 The only thing I can imagine is that there is some kind of misalignment with your time-zones as suggested by @Octavio Herrera.我唯一能想象的是,正如@Octavio Herrera 所建议的那样,您的时区存在某种错位。 But that's difficult to say without knowing a bit more about your environment.但是,如果不了解更多有关您的环境的信息,就很难说。

I had the same problem.我有同样的问题。 Every command was correctly registered but I always received the “No scheduled commands are ready to run.”每个命令都已正确注册,但我总是收到“没有准备好运行的计划命令”。 message.信息。 The problem was that the website was in "maintenance mode" ( php artisan down command) while we were doing updates and tests.问题是在我们进行更新和测试时,网站处于“维护模式”php artisan down命令)。

I think that my blog will help you answer your question.我认为我的博客将帮助您回答您的问题。 Please see the below or link: Laravel Crontab In many projects, you need use crontab (cron jobs) to execute some tasks as sending email or delete waste record in DB.请参阅以下或链接: Laravel Crontab在许多项目中,您需要使用 crontab(cron 作业)来执行一些任务,例如发送电子邮件或删除 DB 中的垃圾记录。 With Laravel Project, you can do this easier.使用 Laravel 项目,您可以更轻松地做到这一点。

Create a command in Laravel 4:在 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),
                );
        }
 
}

Next step, you need to register the command with Laravel CLI.下一步,您需要使用 Laravel CLI 注册命令。 So easy, you open app/start/artisan.php file, and add one line as below:很简单,你打开 app/start/artisan.php 文件,添加如下一行:

Artisan::add(new FirstCommand);

You are done creating Laravel Command.你已经完成了 Laravel 命令的创建。 To test, you could use command below:要测试,您可以使用以下命令:

$ php artisan user:active

User Active The output above mean you successfully register a command. User Active 上面的输出意味着您成功注册了一个命令。

Finally, put your command into the crontab:最后,将您的命令放入 crontab:

crontab -e

Add line (run command every 2 minutes):添加行(每 2 分钟运行一次命令):

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

That's all.就这样。 Thank you for talking time to read this.感谢您花时间阅读本文。

On Windows, I fixed this issue by setting the Scheduled Task to run every minute (even though I only wanted to trigger a command once per day), otherwise I always got the No scheduled commands are ready to run.在 Windows 上,我通过将计划任务设置为每分钟运行一次来解决这个问题(即使我只想每天触发一次命令),否则我总是得到No scheduled commands are ready to run. message.信息。

Since I still ran into this issue 4 years later (2019) and a different workaround worked for me, I think it is worth hinting the simple step that solved for me, which is: Use a shorter interval first.由于我在 4 年后(2019 年)仍然遇到这个问题,并且不同的解决方法对我有用,我认为值得暗示为我解决的简单步骤,即:首先使用较短的间隔。

That seems to just wake it up to handle longer intervals in some ways.这似乎只是唤醒它以在某些方面处理更长的间隔。 I had everyFiveMinutes() and for almost 2 hours it was getting the No scheduled commands are ready to run response.我有everyFiveMinutes()并且在将近 2 个小时的时间里,它得到了No scheduled commands are ready to run响应。 I simply changed it to everyMinute() and it started running correctly.我只是将其更改为everyMinute()并且它开始正常运行。 I watched it consistently for like 10 minutes or so, then changed it back to everyFiveMinutes() and it all went smoothly.我持续观看了大约 10 分钟,然后将其改回everyFiveMinutes()并且一切顺利。

To run the Cron Commands on the local server , follow these steps:要在本地服务器上运行 Cron 命令,请执行以下步骤:

  1. I know you have already mentioned the command in app/console/Kernel.php我知道你已经提到了 app/console/Kernel.php 中的命令
  2. Now open the command line, enter "crontab -e"现在打开命令行,输入“crontab -e”
  3. Edit that file and mention the below code(without quote) to keep running PHP artisan schedule:run in the background编辑该文件并提及以下代码(不带引号)以继续运行 PHP artisan schedule:run in the background

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

  1. Enter "crontab -l" in the command line, it will list running crons在命令行中输入“crontab -l”,它会列出正在运行的 cron

Done !!完毕 !!

Now, wait for cron to process your command.现在,等待 cron 处理您的命令。 Cheers!!干杯!!

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

For whatever reason cron does not recognize the named version of your task.无论出于何种原因,cron 都无法识别任务的命名版本。

So in your schedule instead of writing所以在你的日程表中而不是写作

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

you should use the path of the class, such as您应该使用类的路径,例如

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

...the same goes for the scheduler on Laravel Forge! ... Laravel Forge 上的调度程序也是如此!

I have tried everything but finally I found a solution for this problem.我已经尝试了一切,但最终我找到了解决这个问题的方法。 Add the timestamp in the command.在命令中添加时间戳。 Below is the example for this.下面是一个例子。

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

or或者

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

Try尝试

php artisan cache:clear php 工匠缓存:清除

and then run scheduler again with然后再次运行调度程序

php artisan schedule:run php 工匠时间表:运行

I was also facing same issue and it resolved my problem.我也遇到了同样的问题,它解决了我的问题。

I've stuck with this problem No scheduled commands are ready to run.我一直在解决这个问题No scheduled commands are ready to run. for an hours, but solve it easly:一个小时,但很容易解决:

Problem was with rights to folder storage .问题在于文件夹存储的权限。

So, i've set chmod -R 777 storage/* (i'm not sure is this is elegant way).所以,我已经设置了chmod -R 777 storage/* (我不确定这是不是优雅的方式)。

After that cron starts working properly.之后,cron 开始正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM