Laravel 的任务调度(计划任务)功能

参考文档 http://d.laravel-china.org/docs/5.4/scheduling

解决的问题:不用频繁在服务器操作crontab.

自定义的 Artisan 命令

makde:command order

打开文件:

laravel\app\Console\Commands\order.php

如下修改:
修改为自己的命令和要处理的内容

class order extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:make';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '命令描述';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        Log::info('test');
    }
}

然后修改: laravel\app\Console\Kernel.php 文件

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

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
         $schedule->command('inspire')
                  ->hourly();
         
         $schedule->command('order:make')->everyMinute();
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

启用计划任务:在服务器中加入到计划任务 crontab -e

* * * * * php /home/vagrant/Code/laravel/artisan schedule:run >> /dev/null 2>&1

打开日志文件查看效果

laravel\storage\logs\laravel.log
Laravel 的任务调度(计划任务)功能_第1张图片
image.png

你可能感兴趣的:(Laravel 的任务调度(计划任务)功能)