1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > php artisan 计划任务 Laravel 定时任务 任务调度 可手动执行

php artisan 计划任务 Laravel 定时任务 任务调度 可手动执行

时间:2023-03-24 04:40:52

相关推荐

php artisan 计划任务 Laravel 定时任务 任务调度 可手动执行

1、创建一个命令

php artisan make:command TestCommand

执行成功后会提示:

Console command created successfully.

生成了一个新的命令文件

App\Console\Commands\TestCommand.php

use Illuminate\Console\Command;classTestCommand 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*/

publicfunction __construct()

{

parent::__construct();

}/**

* Execute the console command.

* 处理业务逻辑

* @return int*/

publicfunction handle()

{

echo123123;

echo PHP_EOL;

exit;

}

}

2、配置console的Kernel

use App\Console\Commands\TestCommand;

use Illuminate\Console\Scheduling\Schedule;

use Illuminate\Foundation\Console\KernelasConsoleKernel;classKernel extends ConsoleKernel

{/**

* The Artisan commands provided by your application.

*

* @var array*/

protected $commands =[// TestCommand::class,

];/**

* Define the application's command schedule.

*

* @param \Illuminate\Console\Scheduling\Schedule $schedule

* @return void*/

protectedfunction schedule(Schedule $schedule)

{//$schedule->command('inspire')->hourly();

$schedule->command('test')->everyMinute();

}/**

* Register the commands for the application.

*

* @return void*/

protectedfunction 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');

设置时区

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。