zoukankan      html  css  js  c++  java
  • larave 定时任务

    php artisan make:command SendEmails

     命令该会在  app/Console/Commands 目录下创建³³一个新的命令类

    2然后发现在应用程序/控制台/命令路径下多了一个SendEmails.php的文件

        <?php
        namespace AppConsoleCommands;
        use IlluminateSupportFacadesDB;
        use IlluminateConsoleCommand;
         
        class SendEmails extends Command
        {
            /**
             * The name and signature of the console command.
             *
             * @var string
             */
             //此处代表laravel自动生成的名称,下面执行定时任务的时候能用到
            protected $signature = 'command:name';
         
            /**
             * The console command description.
             *
             * @var string
             */
              //对定时任务的描述并没有什么用的
            protected $description = 'Command description';
         
            /**
             * Create a new command instance.
             *
             * @return void
             */
             //这个是laravel自带的构造方法。初始状态下是空的
            public function __construct()
            {
                parent::__construct();
            }
         
            /**
             * Execute the console command.
             *
             * @return mixed
             */
            public function handle()  
            {
                /*
                 * 这个方法做任务的具体处理,也就是对数据的操作,可以用模型
                 * 例如我要删除用户表ID为1的用户
                 * */
                
                DB::table('user')->where('id',1)->delete();
            }
        }

    3:定时命令创建好之后,我们需要修改应用程序/控制台/ Kernel.php 文件  kernel.php文件里面,主要是定义命令的调度时间,定义命令的执行先后顺序等

      我的kernel.php文件

        <?php
        namespace AppConsole;
        use IlluminateConsoleSchedulingSchedule;
        use IlluminateFoundationConsoleKernel as ConsoleKernel;
         
        class Kernel extends ConsoleKernel
        {
            /**
             * The Artisan commands provided by your application.
             *
             * @var array
             */
            protected $commands = [
          
                //引入我们新创建的类
                 AppConsoleCommandsSendEmails::class
         
            ];
         
            /**
             * Define the application's command schedule.
             *
             * @param  IlluminateConsoleSchedulingSchedule  $schedule
             * @return void
             */
             
            protected function schedule(Schedule $schedule)
            {
             //此处相当于规定同意的定时执行时间,如每分钟分执行以下任务
             // command('command:name')写的要与我们刚刚SendEmails文件里的$signature相同
                $schedule->command('command:name')->everyMinute();
                        
            }
         
            /**
             * Register the commands for the application.
             *
             * @return void
             */
            //这个部分是laravel自动生成的,引入我们生成的命令文件
            protected function commands()
            {
                $this->load(__DIR__.'/Commands');
         
                require base_path('routes/console.php');
            }
        }

    关于具体的调度方法的时间表(),大家可以去laravel文档看看 http://laravelacademy.org/post/8484.html

    4:composer执行命令php artisan command:name  这里执行的command:name就是SendEmails.php/$signature自己这个里面是什么就写什么。

    查看本地是否定时执行成功

    linux准备执行我们的定时:

    先查看服务器上的定时条目 crontab -l

    新增或编辑服务器上的定时条目  crontab -e

    在里面新增我们写好的方法路径

    代表每分钟     蓝线代表的是项目路径  找到artisan 执行

    前面的5个 * 分别代表分钟、小时、天、月、星期。  */1代表每一分钟执行一次
    分钟:0-59的整数,默认和/1 代表1分钟。
    小时:0-23的整数。
    天:1-31的整数。
    月:1-12的整数。
    星期:0-7的整数,0和7都代表星期日。

    这里面新加上我们的定时任务,测试是否成功

    然后执行命令 php artisan schedule:run 将在1分钟后执行脚本

    注意事项:

    第一是规定定时任务的执行时间

    第二是 要把项目的artisan目录路径写对

    第三 schedule:run就是执行咱们之前写的任务调度,也就是kernel.php文件中的schedule方法。

    第四 cron 内容最后一行未回车


    原文链接:https://blog.csdn.net/qq_43638176/article/details/88057425

  • 相关阅读:
    UVA 11925 Generating Permutations 生成排列 (序列)
    UVA 1611 Crane 起重机 (子问题)
    UVA 11572 Unique snowflakes (滑窗)
    UVA 177 PaperFolding 折纸痕 (分形,递归)
    UVA 11491 Erasing and Winning 奖品的价值 (贪心)
    UVA1610 PartyGame 聚会游戏(细节题)
    UVA 1149 Bin Packing 装箱(贪心)
    topcpder SRM 664 div2 A,B,C BearCheats , BearPlays equalPiles , BearSorts (映射)
    UVA 1442 Cave 洞穴 (贪心+扫描)
    UVA 1609 Foul Play 不公平竞赛 (构(luan)造(gao)+递归)
  • 原文地址:https://www.cnblogs.com/ithubb/p/14665981.html
Copyright © 2011-2022 走看看