为什么php artisan:schedule run命令不执行artisan命令? [英] Why the php artisan: schedule run command does not execute the artisan commands?
问题描述
我运行php artisan
:schedule run命令,它显示了消息,表明该命令正在运行.但是,什么也没发生(命令触发的事件),它没有任何作用,那会是什么?
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');
}
}
运行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
注意:如果我分别运行命令 ,则可以正常运行,例如:php artisan Payments:check
Note: if I run the commands separately it works, for example: php artisan payments: check
推荐答案
要在调度程序中使用Command,可以使用它的签名或类名. App\Console\Commands
中的每个命令都具有以下内容:
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";
一旦将命令导入到App\Console\Kernel.php
中的protected $commands = [];
数组中,就可以在schedule()
函数中使用它,但是使用ExampleCommand::class
是不正确的:
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();
...
}
这里的主要问题似乎是--force
选项引发以下错误:
The main issue here seems to be with the --force
option throwing the following error:
-force"选项不存在
"--force" option does not exist
许多现有的Laravel命令都设置了--force
标志,该标志在文档中具有以下作用:
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.
许多工匠命令会在您运行诸如php artisan migrate
之类的命令
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?
由于调度程序是非交互式的,因此--force
标志会将此提示覆盖为是".话虽如此,您需要自己定义和处理该选项:
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.
}
}
}
这未经测试,但是如果在.env
中设置了APP_ENV=production
,并且$force
是null
(如果不包括--force
,则为默认值),它将提示您进行确认,如果否".
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.
这篇关于为什么php artisan:schedule run命令不执行artisan命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!