admin管理员组

文章数量:1794759

PHP lumen/laravel 定时任务 schedule crontab

PHP lumen/laravel 定时任务 schedule crontab

* 查看PHP artisan可用的命令

/opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan help

* 添加crontab任务

crontab -e

 

# Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h  dom mon dow   command  

打开后添加这一行

* * * * * /opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan schedule:run >> /dev/null 2>&1

注意最后面要添加一个空行

crontab: installing new crontab new crontab file is missing newline before EOF, can't install. Do you want to retry the same edit? (y/n) y crontab: installing new crontab

 

列出当前的计划任务

$ crontab -l * * * * * /opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan help schedule:run >> /dev/null 2>&1

 

* 定义artisan命令

E:\\easyeye\\src\\admin\\app\\Console\\Commands\\TestCommand.php

<?php namespace App\\Console\\Commands; use Illuminate\\Console\\Command; use Illuminate\\Support\\Facades\\Log; class TestCommand extends Command { /** * @var string * The console command name. * */ protected $name = 'test:putcache'; /** * @var string * The console command description. */ protected $description = 'test controller'; /** * Execute the console command. * @return mixed */ public function handle() { $now = new \\DateTime('now'); Log::info("schedule task at " . $now->format('Y-m-d H:i:s')); } }

** list artisan commands

/opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan list

** Run the scheduled commands

/opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan schedule:run

** 测试新增的artisan命令是否可用

 /opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan test:putcache

$ tail /storage/logs/lumen-`date +%F`.log  

# 这里lumen log配置路径 

blog.csdn/fareast_mzh/article/details/82799647

默认是在 ./storage/logs/lumen.log

... [2018-09-25 11:02:32] local.INFO: schedule task at 2018-09-25 11:02:32  

说明test:putcache这个artisan命令已经设置成功

 

* 添加lumen调度任务

./app/Console/Kernel.php

<?php namespace App\\Console; use Illuminate\\Console\\Scheduling\\Schedule; use laravel\\Lumen\\Console\\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ \\App\\Console\\Commands\\TestCommand::class ]; /** * Define the application's command schedule. * * @param \\Illuminate\\Console\\Scheduling\\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('test:putcache')->everyMinute(); } }

 

添加计划任务还可以使用闭包

$schedule->call(function () {     Log::info('任务调度'); })->everyMinute();

调度的时间可以有多种:  ->cron(‘* * * * *’); 在自定义Cron调度上运行任务  ->everyMinute(); 每分钟运行一次任务  ->everyFiveMinutes(); 每五分钟运行一次任务  ->everyTenMinutes(); 每十分钟运行一次任务  ->everyThirtyMinutes(); 每三十分钟运行一次任务  ->hourly(); 每小时运行一次任务  ->daily(); 每天凌晨零点运行任务  ->dailyAt(‘13:00’); 每天13:00运行任务  ->twiceDaily(1, 13); 每天1:00 & 13:00运行任务  ->weekly(); 每周运行一次任务  ->monthly(); 每月运行一次任务  还有一下额外的方法,请参考:laravelacademy/post/235.html

 

* 执行计划任务

$ /opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan schedule:run Running scheduled command: '/opt/lampp/bin/php-7.2.8' artisan test:putcache > '/dev/null' 2>&1

 

Reference:

laravel task schedule

laravel/docs/5.6/scheduling

 

 

本文标签: lumenPHPlaravelcrontabschedule