1.先在commands里面创建一个类
php artisan make:command TestTimerTask
2.TestTimerTask里面抒写业务逻辑
public function handle()
{
DB::insert('insert into timed_task(test) values(1)');
}
3.在app/Kernel.php里面调用TestTimerTask
注册:
protected $commands = [ \App\Console\Commands\TestTimedTask::class ];
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('test-timedtask')->everyMinute(); // 设置任务每分钟运行一次;
}
注意:这里的"test-timedtask" 是TestTimerTask类里面的$signature参数
protected $signature = 'test-timedtask';
4.测试定时任务
php artisan schedule:run
5.利用Linux来执行定时任务
crontab -e
* * * * * cd /你的项目路径 && php artisan schedule:run >> /dev/null 2>&1