PHP Laravel timed Task schedule

Source: Internet
Author: User

Premise: This method is to use Linux crontab timed tasks to help implement Laravel scheduling (Mac also).
First, add crontab scheduled Tasks, here only to do a brief introduction.
Add the following with the command crontab-e

/usr/local/bin/php /usr/local/var/www/projectName/artisan schedule:run >> /dev/null 2>&1
    • 1
    • 2

:

The above command crontab every minute to tune the Laravel Schedule command, and then laravel judgment to perform the task.
Note: To be aware of PHP and artisan directories, which PHP can view the PHP directory
***** 你的命令
In addition the preceding command 5 * stands for minutes, hours, days, months, and weeks respectively.
Minutes: 0-59 integers, default * and */1 for 1 minutes.
Hours: An integer of 0-23.
Days: An integer of 1-31.
Month: An integer of 1-12.
Week: 0-7 of integers, 0 and 7 all represent Sunday.
Crontab-l can list the current scheduled tasks.

Second, add Laravel Dispatch task.
1. Define your dispatch task in the App\console\kernel class:

<?phpnamespace App\console;use Illuminate\console\scheduling\schedule;use Laravel\lumen\console\kernel as Consolekernel;use Log;ClassKernelExtendsconsolekernel{/** * The Artisan commands provided by your application. * Define Artisan Command *  @var array */protected $commands = [\app\console\commands\test::class,]; /** * Define the application ' s command schedule. * Define scheduling Tasks * @ Param \illuminate\console\scheduling\schedule $schedule *  @return void */protected function Schedule (schedule $schedule) {//method one: //$schedule->call (function () {//log::info (' Task Scheduler '); //}")->everyminute (); //method Two: $schedule->command ( ' test ')->everyminute ();}}  

The above examples are two implementation methods, one is to use closure, method two is implemented with the artisan command.
There can be a variety of scheduling times:
->cron (' * * * * * * '); To run a task on a custom cron schedule
->everyminute (); Run a task every minute
->everyfiveminutes (); Run a task every five minutes
->everytenminutes (); Run a task every 10 minutes
->everythirtyminutes (); Run a task every 30 minutes
->hourly (); Run a task once per hour
->daily (); Run a task 0 o'clock in the morning every day
->dailyat (' 13:00 '); Run a task 13:00 every day
->twicedaily (1, 13); Run a task every day AT & 13:00
->weekly (); Run a task once a week
->monthly (); Run a task once a month
For additional methods, please refer to: http://laravelacademy.org/post/235.html

Here's how to proceed with 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 =  ' Test:putcache '; /** * the console command description. *  @var String */protected $description =  ' test controller '; /** * Execute the console command. *  @return mixed * * Public fu Nction handle () {//here to do the specific processing of the task, you can use the Model Log::info ( 

Well, the above can perform scheduled tasks, there is a small trick if the above task is not executed can be used to command PHP artisan list can print out some error messages

PHP Laravel timed Task schedule

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.