Skip to content

Instantly share code, notes, and snippets.

@Yangwendaxia
Last active November 24, 2016 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yangwendaxia/00d44f43be15f480971dece5832a613e to your computer and use it in GitHub Desktop.
Save Yangwendaxia/00d44f43be15f480971dece5832a613e to your computer and use it in GitHub Desktop.

#实现定时任务(范例) ##1. php artisan make:console LogInfo ##2. 在项目Console/Commands 文件夹下编辑 LogInfo.php

        protected $signature = 'lesson:log';
        protected $description = 'Log Info';

        public function handle(){
            \Log::info('It Works'); //这里你可以实现自己的定时任务功能逻辑
        }

##3. 在 Kernel.php 文件中注册该命令,在 schedule() 方法中添加上面定义的需要执行的 Console Command

       protected $commands = [\App\Console\Commands\LogInfo::class,];

       protected function schedule(Schedule $schedule)
       {
           $schedule->command('lesson:log')->everyMinute();
       }

##4. 生成定时任务定义文件(终端里执行以下命令)

       echo '* * * * * php /Users/yangwen/laravel51-deep/artisan schedule:run >> /dev/null 2>&1' > cron.txt

/path/to/artisan 的值类似 /Users/yangwen/jiuxiao-oa/artisan。 关于Linux 定时任务命令解读 https://segmentfault.com/a/1190000002955509

##5. 检查系统 cron 运行状态( ubuntu 为例)

/etc/init.d/cron status

##6.启动 cron 如果上一步结果为:

root@05034a881f5d:/app# /etc/init.d/cron status
 * cron is not running

则执行以下命令,否则,跳过本步骤

/etc/init.d/cron start

##7. 在Linux 系统中指定上面的定时任务文件

//执行定时任务
crontab conron.txt

检查 crontab 列表

crontab -l 

删除定时任务

crontab -r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment