我的php artisan cron有一些问题。
当前,任务位于App \ Console \ Commands \ task1.php
中
cron在App \ Console \ Kernel.php
问题是:任务没有启动。
所以我尝试使用命令行创建一个新任务:
php path/to/artisan make:command task2
并创建一个文件,但是在App \ Commands中,而不是在App \ Console \ Commands中。
当尝试使用artisan list
列出现有的cron任务时,我只会得到:
vendor:publish Publish any publishable assets from vendor packages
我应该在哪里放置命令?
内核文件应该在哪里?
当前,Kernel.php是:
<?php namespace App\Console;
use App\Commands\Task2;
use App\Console\Commands\Task1;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel {
protected $commands = [
Task1::class,
Task2::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('task1')->daily();
$schedule->command(Task2::class)->daily();
}
}
Task1(使用命令行在App \ Commands文件夹中创建的任务):
<?php namespace App\Commands;
use Illuminate\Console\Command;
use Illuminate\Contracts\Bus\SelfHandling;
class Task1 extends Command implements SelfHandling {
protected $name = 'task1';
protected $description = 'Task1 test ';
public function handle()
{}
}
Task2,在App \ Console \ Commands文件夹中:
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
class Task2 extends Command {
protected $name = 'task2';
protected $description = 'task 2 test';
public function handle()
{}
}
答案 0 :(得分:2)
您需要$signature
而不是$name
<?php namespace App\Commands;
use Illuminate\Console\Command;
use Illuminate\Contracts\Bus\SelfHandling;
class Task1 extends Command implements SelfHandling {
protected $signature = 'task1';
protected $description = 'Task1 test ';
public function handle()
{}
}
答案 1 :(得分:1)
更改Kernel.php中的
$schedule->command('task2')->daily();
删除
use App\Commands\Task2;
use App\Console\Commands\Task1;
更改
protected $commands = [
Console\Commands\Task1::class,
Commands\Task2::class,
];
并在您的命令中添加$signature
使用artisan make:console
(旧的Laravel)