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配置路徑
預設是在 ./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(); 每月執行一次任務
還有一下額外的方法,請參考:http://laravelacademy.org/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