Laravel 定时任务 任务调度 可手动执行
1、創建一個命令
php artisan make:command TestCommand
執行成功后會提示:
Console command created successfully.
生成了一個新的命令文件
AppConsoleCommandsTestCommand.php
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
class TestCommand extends Command
{
/**
* The name and signature of the console command.
* 命令名稱(執行時需要用到)
* @var string
*/
protected $signature = 'test';
/**
* The console command description.
* 命令描述
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
* 處理業務邏輯
* @return int
*/
public function handle()
{
echo 123123;
echo PHP_EOL;
exit;
}
}
2、配置console的Kernel
<?php
namespace AppConsole;
use AppConsoleCommandsTestCommand;
use IlluminateConsoleSchedulingSchedule;
use IlluminateFoundationConsoleKernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
TestCommand::class,
];
/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command('test')->everyMinute();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
3、執行命令
手動執行:
php artisan test(命令名稱)
自動執行:
php artisan schedule:run
定時執行:crontab添加
php artisan schedule:run
->cron('* * * * *');
自定義 Cron 計劃執行任務
->everyMinute();
每分鐘執行一次任務
->everyFiveMinutes();
每五分鐘執行一次任務
->everyTenMinutes();
每十分鐘執行一次任務
->everyFifteenMinutes();
每十五分鐘執行一次任務
->everyThirtyMinutes();
每三十分鐘執行一次任務
->hourly();
每小時執行一次任務
->hourlyAt(17);
每小時第 17 分鐘執行一次任務
->daily();
每天 0 點執行一次任務
->dailyAt('13:00');
每天 13 點執行一次任務
->twiceDaily(1, 13);
每天 1 點及 13 點各執行一次任務
->weekly();
每周日 0 點執行一次任務
->weeklyOn(1, '8:00');
每周一的 8 點執行一次任務
->monthly();
每月第一天 0 點執行一次任務
->monthlyOn(4, '15:00');
每月 4 號的 15 點 執行一次任務
->quarterly();
每季度第一天 0 點執行一次任務
->yearly();
每年第一天 0 點執行一次任務
->timezone('America/New_York');
設置時區
總結
以上是生活随笔為你收集整理的Laravel 定时任务 任务调度 可手动执行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 判断某一天是这一年的第多少天
- 下一篇: 学习通回应用户数据泄露