laravel 之实现定时任务

使用我们上一课创建的artisanShowTime
http://www.httproot.com/lean/16

  1. Console/Kernel.phpschedule方法中注册上我们需要执行的任务

     protected function schedule(Schedule $schedule)
         {
             $schedule->command('inspire')
                      ->hourly();
             $schedule->command('show: time:123')
             ->everyMinute();
         }
    

everyMinute();//每一分钟
daily()//每一天
dailyAt(‘12:00’)//指定每天的时间
…挺多的暂时我先不都写上了

  1. 命令行执行php artisan schedule:run
     Running scheduled command: '/usr/bin/php' 'artisan' show: time:123 > '/dev/null' 2>&1 &
    

看到这样的输入证明成功了.
到这告一个段落 但是还没有实现定时执行的目的,

3.首先执行

    echo '* * * * * php /data/wwwroot/test/artisan schedule:run 1>> /dev/null 2>&1' >run.txt

然后pwd 看下项目所在的位置
复制位置
vim run.txt 设置文件路径
cat run.txt 确认有没问题
crontab run.txtcrontab 加载定时任务
crontab -l执行定时任务
crontab -r删除定时任务

注意:
先确保php artisan schedule:run运行成功

可以打印日志:
\Log::info('httproot:'.time().$this->argument('name'));

HTTPROOT | 自学PHP | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 石头哥 |微信小程序 |木讯 |备案
Copyright © 1998 - 2016 HTTPROOT.COM. All Rights Reserved httproot.com 版权所有