zoukankan      html  css  js  c++  java
  • laravel创建定时任务

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

    英文文档: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 AppConsoleCommands;
    
    use IlluminateConsoleCommand;
    use IlluminateSupportFacadesLog;
    
    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 AppConsole;
    
    use IlluminateConsoleSchedulingSchedule;
    use IlluminateFoundationConsoleKernel as ConsoleKernel;
    use IlluminateSupportFacadesLog;
    
    class Kernel extends ConsoleKernel
    {
        /**
         * The Artisan commands provided by your application.
         *
         * @var array
         */
        protected $commands = [
            // CommandsInspire::class,
            CommandsTestConsole::class
        ];
    
        /**
         * Define the application's command schedule.
         *
         * @param  IlluminateConsoleSchedulingSchedule  $schedule
         * @return void
         */
        protected function schedule(Schedule $schedule)
        {
            // $schedule->command('inspire')
            //          ->hourly();
            Log::info('zddddd');
            //调用artisan
            $schedule->command('testconsole')->everyMinute();
        }
    }

    有问题欢迎留言交流。

    技术交流群:576269252

    --------------------------------------

    声明: 原创文章,未经允许,禁止转载!

    --------------------------------------

  • 相关阅读:
    Autofac 学习简易教程随笔(一)
    实现Entity Framework SQLite Code First 开发
    Entity Framework SQLite 开发及实现简单的自定义Migration Engine
    MSSQLServer和SQL Server Express、LocalDB的区别
    .gitignore文件
    Entity Framework MSSQL Code First 开发
    页面为要加<!DOCTYPE html>
    数字图像处理(下)
    数字图像处理(上)
    列表
  • 原文地址:https://www.cnblogs.com/xxoome/p/6092961.html
Copyright © 2011-2022 走看看