问题
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.
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
.
I know it works because, I've Log::info('Working')
at the entry of the handle()
function of the command.
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.
But the artisan command not executed, it output nothing to the console, and not write something to the DB
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();
}
}
Command name, protected $signature = 'command:printer-serving {pid}';
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.
I want to know how to debug the $schedule->command()
function.
Cron file 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
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
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.
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.
回答1:
I think you should be using the below command
$schedule->command('printer_serving')->everyMinute();
回答2:
It costs me a day to google the issue, read laravel docs, and run many test. Finally I make it out.
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).
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.
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).
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.).
来源:https://stackoverflow.com/questions/47548220/laravel-cannot-run-scheduled-command-while-queuelisten-run-schedule-periodic