Laravel 定时任务 任务调度 可手动执行
1、創(chuàng)建一個(gè)命令
php artisan make:command TestCommand
執(zhí)行成功后會提示:
Console command created successfully.
生成了一個(gè)新的命令文件
AppConsoleCommandsTestCommand.php
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
class TestCommand extends Command
{
/**
* The name and signature of the console command.
* 命令名稱(執(zhí)行時(shí)需要用到)
* @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.
* 處理業(yè)務(wù)邏輯
* @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、執(zhí)行命令
手動(dòng)執(zhí)行:
php artisan test(命令名稱)
自動(dòng)執(zhí)行:
php artisan schedule:run
定時(shí)執(zhí)行:crontab添加
php artisan schedule:run
->cron('* * * * *');
自定義 Cron 計(jì)劃執(zhí)行任務(wù)
->everyMinute();
每分鐘執(zhí)行一次任務(wù)
->everyFiveMinutes();
每五分鐘執(zhí)行一次任務(wù)
->everyTenMinutes();
每十分鐘執(zhí)行一次任務(wù)
->everyFifteenMinutes();
每十五分鐘執(zhí)行一次任務(wù)
->everyThirtyMinutes();
每三十分鐘執(zhí)行一次任務(wù)
->hourly();
每小時(shí)執(zhí)行一次任務(wù)
->hourlyAt(17);
每小時(shí)第 17 分鐘執(zhí)行一次任務(wù)
->daily();
每天 0 點(diǎn)執(zhí)行一次任務(wù)
->dailyAt('13:00');
每天 13 點(diǎn)執(zhí)行一次任務(wù)
->twiceDaily(1, 13);
每天 1 點(diǎn)及 13 點(diǎn)各執(zhí)行一次任務(wù)
->weekly();
每周日 0 點(diǎn)執(zhí)行一次任務(wù)
->weeklyOn(1, '8:00');
每周一的 8 點(diǎn)執(zhí)行一次任務(wù)
->monthly();
每月第一天 0 點(diǎn)執(zhí)行一次任務(wù)
->monthlyOn(4, '15:00');
每月 4 號的 15 點(diǎn) 執(zhí)行一次任務(wù)
->quarterly();
每季度第一天 0 點(diǎn)執(zhí)行一次任務(wù)
->yearly();
每年第一天 0 點(diǎn)執(zhí)行一次任務(wù)
->timezone('America/New_York');
設(shè)置時(shí)區(qū)
總結(jié)
以上是生活随笔為你收集整理的Laravel 定时任务 任务调度 可手动执行的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 判断某一天是这一年的第多少天
- 下一篇: 安排!