Home > PHP Framework > Laravel > PHP Laravel scheduled task Schedule [dry information]

PHP Laravel scheduled task Schedule [dry information]

藏色散人
Release: 2020-06-23 13:43:30
forward
5343 people have browsed it

The following is the tutorial column of Laravel to introduce PHP Laravel scheduled task Schedule. I hope it will be helpful to friends who need it!

PHP Laravel scheduled task Schedule [dry information]

Premise: The method in this article is to use Linux's crontab scheduled task to assist in implementing Laravel scheduling (the same is true for Mac).

1. First add the Crontab scheduled task. Here is just a brief introduction.

Use the command crontab -e to add the following content

* * * * * /usr/local/bin/php /usr/local/var/www/projectName/artisan schedule:run >> /dev/null 2>&1
Copy after login

As shown:

PHP Laravel scheduled task Schedule [dry information]

The above command Crontab will adjust every minute Laravel's schedule command, and then Laravel determines the execution task.

Note: Pay attention to the directories of php and artisan, which php can view the php directory

***** Your command

In addition, the above The five * in front of the command represent minutes, hours, days, months, and weeks respectively.

Minute: an integer from 0-59, the default * and */1 represent 1 minute.

Hour: an integer from 0 to 23.

Day: an integer from 1 to 31.

Month: an integer from 1 to 12.

Weekday: an integer from 0 to 7, both 0 and 7 represent Sunday.

crontab -l can list the current scheduled tasks.

2. Add Laravel scheduling task.

1. Define your scheduling task in the App\Console\Kernel class:

<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
use Log;
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     * 定义Artisan命令
     * @var array
     */
    protected $commands = [
                \App\Console\Commands\test::class,
    ];
    /**
     * Define the application&#39;s command schedule.
     * 定义调度任务
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        //方法一:
//            $schedule->call(function () {
//                Log::info(&#39;任务调度&#39;);
//            })->everyMinute();
        //方法二:
        $schedule->command(&#39;test&#39;)->everyMinute();
    }
}
Copy after login

The above gives two examples of implementation methods. The first method is to use closures, and the second method is Implemented using Artisan commands.

There are many scheduling times:

->cron(‘* * * * *’); 在自定义Cron调度上运行任务
->everyMinute(); 每分钟运行一次任务
->everyFiveMinutes(); 每五分钟运行一次任务
->everyTenMinutes(); 每十分钟运行一次任务
->everyThirtyMinutes(); 每三十分钟运行一次任务
->hourly(); 每小时运行一次任务
->daily(); 每天凌晨零点运行任务
->dailyAt(‘13:00’); 每天13:00运行任务
->twiceDaily(1, 13); 每天1:00 & 13:00运行任务
->weekly(); 每周运行一次任务
->monthly(); 每月运行一次任务
Copy after login

There are additional methods, please refer to: http://laravelacademy.org/post/235.html

Continue the operation of method two:

Third, define the method of Artisan command:

<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Log;
class test extends Command {
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = &#39;test:putcache&#39;;
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = &#39;test controller&#39;;
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
            //这里做任务的具体处理,可以用模型
            Log::info(&#39;任务调度&#39;.time());
    }
}
Copy after login

Okay, the above can execute the scheduled task , there is a little trick. If the above tasks are not executed, you can use the command php artisan list to print out some error messages.

For more laravel related technical articles, please visit the laravel column!

The above is the detailed content of PHP Laravel scheduled task Schedule [dry information]. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template