(1)在这个层级下创建命令文件,直接复制框架自带的Inspire文件改名字也可以,如果代码中调用了其他控制器,记得继承父类:
例:
<?php namespace App\Console\Commands; use App\Http\Controllers\PhoneController; use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; class Updatecache extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'updatecache'; /** * The console command description. * * @var string */ protected $description = 'Update cache for PhoneController follow_statistics four status users'; /** * Create a new command instance. * * @return void */ protected $app; public function __construct() { parent::__construct(); } public function handle() { Log::info("开始缓存客户数据"); $Phone=new PhoneController(); $Phone->follow_statistics_all_cache(); Log::info("完成缓存客户数据"); } }
protected $signature = 'updatecache'; 是COMMAND命令的名字,小写;
(2)之后在Kernel.php中注册命令:
(3)后台用PHP crontab 定时任务调用php artisan 命令的名字调用即可(这样比起一直运行php artisan schedule:run 好得多,只不过如果需要好几个时间段运行需要写成好几个定时任务);
#添加代码 * * * * * /usr/bin/php7.0 /var/www/html/laravel/artisan 命令的名称 注意:/usr/bin/php7.0为你的php位置 ,* * * * *分别代表 分 时 日 月 周 (定时任务的时间) /var/www/html/laravel/为你的项目位置