參考文檔 http://d.laravel-china.org/docs/5.4/scheduling
解決的問題:不用頻繁在服務(wù)器操作crontab.
自定義的 Artisan 命令
makde:command order
打開文件:
laravel\app\Console\Commands\order.php
如下修改:
修改為自己的命令和要處理的內(nèi)容
class order extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'order:make';
/**
* The console command description.
*
* @var string
*/
protected $description = '命令描述';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
Log::info('test');
}
}
然后修改: laravel\app\Console\Kernel.php
文件
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
Commands\order::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->hourly();
$schedule->command('order:make')->everyMinute();
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
啟用計(jì)劃任務(wù):在服務(wù)器中加入到計(jì)劃任務(wù) crontab -e
* * * * * php /home/vagrant/Code/laravel/artisan schedule:run >> /dev/null 2>&1
打開日志文件查看效果
laravel\storage\logs\laravel.log
image.png