Vinicius Aquino
Vinicius Aquino

Reputation: 737

Why the php artisan: schedule run command does not execute the artisan commands?

I run the php artisan: schedule run command and it shows the messages saying that the commands are running. However, nothing happens (the events the commands trigger), it did not work, what can it be?

Kernel.php

<?php

    namespace App\Console;

    use App\Console\Commands\CheckPayments;
    use App\Console\Commands\CheckSubscriptions;
    use App\Console\Commands\DeleteOperationalLogs;
    use App\Console\Commands\GenerateInvoices;
    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

    class Kernel extends ConsoleKernel
    {
        protected $commands = [
            CheckPayments::class,
            GenerateInvoices::class,
            CheckSubscriptions::class,
            DeleteOperationalLogs::class
        ];

        protected function schedule(Schedule $schedule)
        {
            $schedule->command(CheckPayments::class, ['--force'])->everyMinute();
            $schedule->command(GenerateInvoices::class, ['--force'])->everyMinute();
            $schedule->command(CheckSubscriptions::class, ['--force'])->everyMinute();
            $schedule->command(DeleteOperationalLogs::class, ['--force'])->everyMinute();
        }

        protected function commands()
        {
            $this->load(__DIR__.'/Commands');

            require base_path('routes/console.php');
        }
    }

After run php artisan schedule:

Running scheduled command: "C:\xampp\php\php.exe" "artisan" payments:check --force > "NUL" 2>&1
    Running scheduled command: "C:\xampp\php\php.exe" "artisan" subscriptions:check --force > "NUL" 2>&1
    Running scheduled command: "C:\xampp\php\php.exe" "artisan" invoices:generate --force > "NUL" 2>&1
    Running scheduled command: "C:\xampp\php\php.exe" "artisan" logs:delete --force > "NUL" 2>&1

Note: if I run the commands separately it works, for example: php artisan payments: check

Upvotes: 1

Views: 4356

Answers (2)

Waqar Sharif
Waqar Sharif

Reputation: 101

Try this

php artisan schedule:work

Upvotes: 1

Tim Lewis
Tim Lewis

Reputation: 29258

To use a Command in your scheduler, you can use it's signature, or it's Classname. Each Command in App\Console\Commands has the following:

/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = "example:command";

Once the Command is imported into App\Console\Kernel.php, in the protected $commands = []; array, it can be used in the schedule() function, but using ExampleCommand::class isn't correct:

protected function schedule(Schedule $schedule){
  $schedule->command("example:command --force")->everyMinute();
  $schedule->command(ExampleCommand::class, ["--force"])->everyMinute();
  ...
}

The main issue here seems to be with the --force option throwing the following error:

"--force" option does not exist

Many of the existing Laravel commands have the --force flag set, which, from the documentation does the following:

Force the operation to run when in production.

Many artisan commands prompt for input when you run a command, like php artisan migrate, that asks

Are you sure you want to run this command in production?

Since the scheduler is non-interactive, the --force flag will override this prompt to "Yes". That all being said, you need to define and handle the option yourself:

protected $signature = "example:command {--force}";

public function handle(){
  $force = $this->option("force");
  if(env("APP_ENV", "production") == "production" && !$force){
    if(!$this->ask("Are you sure you want to run this in production?")){
      return false; // or dd();, etc.
    }
  } 
}

This is untested, but if APP_ENV=production is set in .env, and $force is null (default if --force isn't included), then it will prompt for confirmation, and exit if "No" is answered.

Upvotes: 2

Related Questions