您的位置:首页 > 编程语言 > PHP开发

laravel框架 任务调度(定时执行任务)

2017-07-11 10:42 369 查看

laravel 任务调度(定时执行任务)

任务调度写在  
app/Console/Kernel.PHP 文件 
schedule
中,里面默认有一个例子。在 schedule 方法里放入自己的执行的代码。比如
这个是每一分钟在数据库里插入一条数据。
[html]<?phpnamespace App\Console;use Illuminate\Console\Scheduling\Schedule;use Illuminate\Foundation\Console\Kernel as ConsoleKernel;use Illuminate\Support\Facades\DB;class Kernel extends ConsoleKernel{/*** The Artisan commands provided by your application.** @var array*/protected $commands = [\App\Console\Commands\Inspire::class,];/*** Define the application's command schedule.** @param \Illuminate\Console\Scheduling\Schedule $schedule* @return void*/protected function schedule(Schedule $schedule){$schedule->exec($schedule->call(function () {DB::table('ceshi')->insert(['contents'=>'新的数据']);})->everyMinute())->daily();}}写完了,还需要定时执行怎么办?借助Liunx的crontab 来定时执行执行,crontab -e 此时显示出现这样的界面,就是进入crontab里面了,接着在最下面写入
* * * * * php 项目的路径/artisan schedule:run >> /dev/null 2>&1
按下 Ctrl + X 保存退出
这样就OK ,如果不执行,请下检查代码,以及项目的路径是否正确。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息