0

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.

LF00
  • 27,015
  • 29
  • 156
  • 295
  • can you create another command which does the same task but without any space between the words – Dhaval Chheda Nov 29 '17 at 08:54
  • I've modify the command, and use default parameter in the command. Then use `$schedule->command('command:printer_serving')->everyMinute(); `, but it also not works. – LF00 Nov 29 '17 at 09:00
  • is it working directly , like php artisan printer_serving – Dhaval Chheda Nov 29 '17 at 09:02
  • Yes, it works well. – LF00 Nov 29 '17 at 09:03
  • is this the only cron job that is failing? – Dhaval Chheda Nov 29 '17 at 09:04
  • 1
    Possible duplicate of [Command schedule in Laravel](https://stackoverflow.com/questions/47513845/command-schedule-in-laravel) – Troyer Nov 29 '17 at 09:04
  • what is the name of the command? – Dhaval Chheda Nov 29 '17 at 09:05
  • @Troyer, read my question once more. The command in my question not run which is different with that one. – LF00 Nov 29 '17 at 09:06
  • @DhavalChheda I can run the command from the console, and I've add the commandname in the question. – LF00 Nov 29 '17 at 09:08
  • Have u configured the cron setup in your server? – Dhaval Chheda Nov 29 '17 at 09:24
  • @DhavalChheda certainly, as the question say, the schedule works well. For the `Log` function in the Kernel.php output content to the console continuously. – LF00 Nov 29 '17 at 09:25
  • Looks like the Cron configuration is executing the schedule every second, that's why you are getting a lot of registers inside the Log, you should put the Log::info inside the command because when the schedule function is called doesn't mean it will execute the commands. – Troyer Nov 29 '17 at 10:02
  • 1
    Instead of `$schedule->command('command:printer-serving --force 281H28')->everyMinute();` try: `$schedule->exec(' sudo -u www-data -p password /var/www/market/artisan command:printer_serving 281H28')->everyMinute();` – Troyer Nov 29 '17 at 10:05
  • @Troyer I have tried it, it not works. I also try this `$schedule->call(function(){Log::info('ssssssss');})->everyMinute();` without any success. – LF00 Nov 29 '17 at 10:27

2 Answers2

1

I think you should be using the below command

$schedule->command('printer_serving')->everyMinute()‌​;
Dhaval Chheda
  • 4,637
  • 5
  • 24
  • 44
  • This not works for me. As the command name is `'command:printer-serving {pid}'`, and from the console I need to run `sudo -u www-data /var/www/market/artisan command:printer_serving 281H28` – LF00 Nov 29 '17 at 09:14
0

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.).

LF00
  • 27,015
  • 29
  • 156
  • 295