当前位置:网站首页>Laravel scheduled task
Laravel scheduled task
2022-07-21 02:49:00 【Xiang Yu it】
1. Create a custom Command
php artisan make:command Test
2. edit Test.php file (app/Console/Commands/Test.php)
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class Test extends Command
{
/** * name * Carry out orders : php artisan Test:command * @var string */
protected $signature = 'Test:command';
// describe
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
// Business performed
public function handle()
{
// Calling method
$this->run_perform();
// Or write logic directly
//Log::info(time().' Test timed tasks ');
}
private function run_perform()
{
// Logic code
}
}
3. modify App\Console\Kernel.php
<?php
namespace App\Console;
use App\Console\Commands\Hello;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
// Custom in application Artisan command , Can be more
protected $commands = [
];
// Define scheduled tasks and execution events
protected function schedule(Schedule $schedule)
{
// Execute every minute with the name command:ceshi The task of
$schedule->command('Test:command')->withoutOverlapping()->everyMinute();
or
$schedule->command(Test::class)->withoutOverlapping()->everyMinute();
// Working day ( Monday to Friday ) 8 spot to 17 Point every hour
//$schedule->command(Test ::class)
// ->weekdays()
// ->hourly()
// ->timezone('America/Chicago')
// ->between('8:00', '17:00');
}
protected function commands()
{
$this->load(__DIR__ . '/Commands');
require base_path('routes/console.php');
}
}
Call frequency and limits
Method | describe |
---|---|
->cron(‘* * * * *’); | Customize Cron Plan to carry out the task |
->everyMinute(); | Perform the task once a minute |
->everyFiveMinutes(); | Every five minutes |
->everyTenMinutes(); | Every ten minutes |
->everyFifteenMinutes(); | Perform the task every 15 minutes |
->everyThirtyMinutes(); | Every 30 minutes |
->hourly(); | Every hour |
->hourlyAt(17); | Every hour 17 One task per minute |
->daily(); | Every day 0 Click to perform a task |
->dailyAt(‘13:00’); | Every day 13 Click to perform a task |
->twiceDaily(1, 13); | Every day 1 Point and 13 Each point performs a task |
->weekly(); | Every Sunday 0 Click to perform a task |
->weeklyOn(1, ‘8:00’); | On Monday 8 Click to perform a task |
->monthly(); | On the first day of every month 0 Click to perform a task |
->monthlyOn(4, ‘15:00’); | monthly 4 The no. 15 spot Perform a task |
->quarterly(); | First day of each quarter 0 Click to perform a task |
->yearly(); | On the first day of every year 0 Click to perform a task |
->timezone(‘America/New_York’) | Set time zone |
->weekdays(); | Limit tasks to working days |
->weekends(); | Limit the task to the weekend |
->sundays(); | Limit the task to Sunday |
->mondays(); | Limit the task to Monday |
->tuesdays(); | Limit the mission to Tuesday |
->wednesdays(); | Limit the mission to Wednesday |
->thursdays(); | Limit the mission to Thursday |
->fridays(); | Limit the mission to Friday |
->saturdays(); | Limit the mission to Saturday |
->between($start, $end); | Limit the task to $start and $end Interval execution ->hourly()->between(‘7:00’, ‘22:00’); |
->when(Closure); | Restrict task execution when closure returns true |
->environments($env); | Restrict tasks to specific environments |
->withoutOverlapping(); | By default , Even if the previous task instance is still executing , Tasks within the schedule will also be executed . To avoid this |
->runInBackground() | By default , Multiple commands scheduled to run simultaneously will be executed sequentially . If you have long running commands , This may cause subsequent commands to start later than expected . You can use runInBackground Method to make the command run in the background |
->onOneServer() | Your application runs on multiple servers , You may need to restrict a scheduled task to only run on a single server |
For other uses, you can check the official documents | https://learnku.com/docs/laravel/7.x/scheduling/7492#introduction |
4. Perform scheduled tasks
Perform a single task manually :
php artisan Test:command ( name )
The server automatically executes :
1. Edit scheduled tasks :
crontab -e
2.php Multiple versions can php Change to the absolute path of the version , Project path
* * * * * php /www/wwwroot/ceshi/artisan schedule:run >> /www/wwwroot/ceshi/test.txt 2>&1
Or do not print the log
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
3. Restart the server
CentOS7 Method restart service
systemctl restart crond.service
CentOS6 Method restart service
service crond restart
边栏推荐
- 从0到1建设智能灰度数据体系:以vivo游戏中心为例
- EF core learning notes: additional foreign key attributes / single navigation attributes
- Password key hard coding check
- Excel宏是什么?Excel宏的使用教程
- DiFi: A Go-as-You-Pay Wi-Fi Access System 精读笔记(三)
- Why should Apple change objc
- 密码密钥硬编码检查
- [Muduo] build project compile cmake file and noncopyable
- How to convert Excel to word format? Method of converting Excel to word format
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
猜你喜欢
Fiddler5+ lightning simulator 4.0 settings for app packet capturing
The beta version of typera is expired and cannot be used normally
Apache Flink's yarn session submission process
全栈代码测试覆盖率及用例发现系统的建设和实践
Password key hard coding check
得物客服一站式工作台卡顿优化之路
Ilrunitme foreach has GC
EF Core学习笔记:额外的外键属性 / 单项导航属性
Relationship between accuracy, recall and confidence
NFT fashion shoes ar interactive zero foundation tutorial is coming!
随机推荐
开发中常见环境配置名词-dev、sit、pro、fac等
How to delete different text in Excel spreadsheet in batch?
[Muduo log system 1] logger output
excel数据条怎么设置百分比颜色?excel数据条按百分比自动填充颜色教程
What are the advantages of building websites with old domain names?
[Android开发学iOS系列] 语言篇: Swift vs Kotlin
EF core learning notes: additional foreign key attributes / single navigation attributes
Calculate the date a few days ago from today
Buying insight and channel evaluation help optimize marketing decisions
Detailed decision tree and random forest
AI2(App Inventor 2) 离线版
华为通用卡证识别功能,一键实现多种卡绑定
How to protect user privacy without password authentication?
MongoDB数据库简介、安装和基本使用
How to configure access control in Gerrit system
[flower carving experience] 20 Music Visualization: esp32_ Series attempts of C3 and ws2812b
Date range generator
Calculate the date after adding the given number of working days
Check whether the date is valid
[Muduo socket] InetAddress encapsulates the socket address type