码迷,mamicode.com
首页 > 其他好文 > 详细

laravel创建定时任务

时间:2016-11-23 12:41:31      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:理解   img   start   end   doc   命令   变量   http   服务器   

官方文档给出的教程已经很详细了,这里给出一些补充帮助大家理解。

英文文档:https://laravel.com/docs/5.2/scheduling

中文文档:https://laravel-china.org/docs/5.2/scheduling

 

Starting The Scheduler

这里文档说的很简单,就是让你在服务器的crontab加入一条命令。

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

关于crontab可以参考这篇文章:http://www.cnblogs.com/xxoome/p/6091459.html

这条命令什么意思呢?按照crontab配置文件格式的解释

技术分享

红框内都是shell命令。

##如果配有配置php的全局环境变量,这里需要指定php的绝对路径
php:/usr/local/php/bin/php

##就是你项目根目录下的artisan文件的绝对路径
artisan:/home/prj-test/test/artisan

例如:

* * * * * /usr/local/php/bin/php /home/prj-test/test/artisan schedule:run >> /dev/null 2>&1

========================== 我是分割线 ===========================

1、创建artisan命令行

文档地址:https://laravel-china.org/docs/5.2/artisan

php artisan make:console TestConsole
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class TestConsole extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = ‘testconsole;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = ‘这是一个测试artisan的描述;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Log::info(‘这是我写的log‘);
    }
}

 

2、编写Kernel

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Log;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        // Commands\Inspire::class,
        Commands\TestConsole::class
    ];

    /**
     * Define the application‘s command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command(‘inspire‘)
        //          ->hourly();
        Log::info(‘zddddd‘);
        //调用artisan
        $schedule->command(‘testconsole‘)->everyMinute();
    }
}

 

有问题欢迎留言交流。

技术交流群:576269252

 

laravel创建定时任务

标签:理解   img   start   end   doc   命令   变量   http   服务器   

原文地址:http://www.cnblogs.com/xxoome/p/6092961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!