laravel无法运行计划的命令,而queue:定期运行schedule() [英] laravel cannot run scheduled command, while queue:listen run schedule() periodically
问题描述
注意:我的问题与 Laravel中的命令时间表无关,该时间表不起作用.但是在我的问题中,调度有效,但是它不能调用artisan命令.
Note: My question has nothing to do with Command schedule in Laravel which schedule not work. But in my question the scheduling works, but it cannot call the artisan command.
我使用laravel调度工匠命令.我像这样sudo -u www-data /var/www/market/artisan command:printer-serving 281H28
从控制台直接运行命令.
I use laravel scheduling artisan command. I run the command directly from the console like this sudo -u www-data /var/www/market/artisan command:printer-serving 281H28
.
我知道它是有效的,因为我在命令的handle()
函数条目处输入了Log::info('Working')
.
I know it works because, I've Log::info('Working')
at the entry of the handle()
function of the command.
当我使用laravel的计划时. cron运作良好,因为Log::info('command:printer-serving 281H28');
以下的内容连续输出到控制台.
While when I use laravel's scheduling. And the cron works well, for below Log::info('command:printer-serving 281H28');
output the content to the console continuously.
但是artisan命令没有执行,它没有向控制台输出任何内容,也没有向DB写东西
在Kernel.php中
In the Kernel.php
<?php namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Log;
class Kernel extends ConsoleKernel {
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
'App\Console\Commands\Inspire',
'App\Console\Commands\CommandPrinterServing',
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')->hourly();
Log::info('command:printer-serving 281H28');
$schedule->command('command:printer-serving --force 281H28')->everyMinute();
//$schedule->command('printer-serving')->everyMinute();
//$schedule->command(CommandPrinterServing::class, ['281H28'])->everyMinute();
}
}
命令名称,protected $signature = 'command:printer-serving {pid}';
注意
不管什么字符串,即使不是我在$schedule->command()
函数中输入的命令,也都不会改变,也不会从cron日志或laravel日志中报告错误.
Note
No matter what string even not a command I put in $schedule->command()
function, nothing will changes and not a error reported from the cron log or laravel log.
我想知道如何调试$schedule->command()
函数.
I want to know how to debug the $schedule->command()
function.
Cron文件vi /etc/cron.minutely/printer-task-minute
#!/bin/sh
cd /var/www/market
sudo -u www-data ./artisan command:regen-htaccess
#sudo -u www-data ./artisan schedule:run >> /dev/null 2>&1
sudo -u www-data ./artisan schedule:run
奇怪的是,日志输出每3秒输出4个thme.
The weired thing is log output four thmes every 3 seconds.
> [2017-11-29 16:52:14] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:14] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:14] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:14] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:17] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:17] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:17] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:17] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:21] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:21] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:21] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:21] worker_env.INFO: command:printer-serving 281H28
> [2017-11-29 16:52:24] worker_env.INFO: command:printer-serving 281H28
我对日志频率感到困惑,所以我停止了cron和beantalkd.但是日志输出不会停止.甚至我停止了apache2,该日志保留输出.然后,我检查了php进程,并在命令ps aux | grep php
的输出中找到了四个queue:listen --queue=xxxx --env=worker_env --delay=3
.在这里,我找到了为什么以该频率输出日志,但我不知道为什么queue:listen
执行schedule()
函数作为此问题了解Laravel 5.2上的队列和调度程序.
I'm puzzled about the log frequency, so I stop cron and beanstalkd. But the log output doesn't stop. Even I stop apache2, the log keeping output. Then I check the php process, and find there are four queue:listen --queue=xxxx --env=worker_env --delay=3
in the output of command ps aux | grep php
. Here I found why the log output in that frequency, but I don't know why queue:listen
execute the schedule()
function as this question Understanding Queues and Scheduler on Laravel 5.2.
每次执行工匠命令sudo -u www-data /var/www/market/artisan xxxxx
时,都会调用schedule()
函数.如果命令是queue:listen
(如sudo -u www-data /var/www/market/artisan queue:listen xxxxx
),则将定期调用schedule()
函数.但是schedule()
中的命令将无法运行,但Log::info()
除外.仅当运行schedule:run
命令时,工匠命令和schedule()
中的Log::info()
都将执行.
Each time I executed a artisan command sudo -u www-data /var/www/market/artisan xxxxx
the schedule()
function will be called one time. And if the command is queue:listen
like sudo -u www-data /var/www/market/artisan queue:listen xxxxx
the schedule()
function will be called periodically. But the command in the schedule()
will not run, except the Log::info()
. Only when run schedule:run
command, both the artisan command and Log::info()
in schedule()
will executed.
推荐答案
花费一天的时间来搜索该问题,阅读laravel文档并进行许多测试.终于我明白了.
It costs me a day to google the issue, read laravel docs, and run many test. Finally I make it out.
Laravel的artisan queue:listen
命令会定期在Kernel.php中调用schedule()
,而它只在schedule()
中运行Log::info()
而不是$schedule->command()
(这里我也很困惑).
Laravel's artisan queue:listen
command call the schedule()
in the Kernel.php periodically, while it only run the Log::info()
in the schedule()
not the $schedule->command()
(Here I'm also confused).
我有四个正在运行的队列,用于队列1 sudo -u www-data php artisan queue:work --queue=queue1 --env=worker_env --delay=3 --timeout=600
.与queue2、3、4相同.因此schedule()
中的Log::info()
将每3秒执行四次.
I've four queue running, for queue1 sudo -u www-data php artisan queue:work --queue=queue1 --env=worker_env --delay=3 --timeout=600
. Same with queue2, 3, 4. So the Log::info()
in the schedule()
will executed four times every 3 seconds.
上面让我觉得时间表很好.实际上,不执行计划.我发现/etc/cron.minutely/printer-task-minute
没有任何x
特权,因此我为其添加了x
特权.我发现我没有配置crontab文件,所以我将* * * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.minutely )
添加到/etc/crontab
(我不熟悉cron).
Above make me thought the schedule works well. Actually, the schedule is not executed. I found /etc/cron.minutely/printer-task-minute
have no x
privilege, so I add the x
privilege for it. And I found I didn't config the crontab file, so I add * * * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.minutely )
to /etc/crontab
(I'm not familar with cron).
解决方案:
像上面一样配置cron,或参考手册.
将queue:listen
更改为queue:work
(当更改为queue:work
时,schedule()
不会周期性地由队列执行,我在这里也不知道原因.).
Solution:
Config the cron like above, or refer to the manual.
Change queue:listen
to queue:work
(When changed to queue:work
, schedule()
will not executed by queue periodically, I also don't know the reason here.).
这篇关于laravel无法运行计划的命令,而queue:定期运行schedule()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!