Laravel框架定时任务2种实现方式示例


Posted in PHP onDecember 08, 2018

本文实例讲述了Laravel框架定时任务2种实现方式。分享给大家供大家参考,具体如下:

第一种

1、生成一个commands文件

> php artisan make:command test

2、打开文件进行修改

laravel\App\Console\Commands\test.php

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class test extends Command
{
 /**
  * The name and signature of the console command.
  *
  * @var string
  */
 protected $signature = 'test:insert'; // php artisan list 中将会生成 "php artisan test:insert " 指令
 /**
  * The console command description.
  *
  * @var string
  */
 protected $description = 'insert Test table some test data'; // 对上面指令的描述
 /**
  * Create a new command instance.
  *
  * @return void
  */
 public function __construct()
 {
  parent::__construct();
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
  // 编写你要的定时任务执行的代码!
  # eg
 Log::info('test');
 }
}

> php artisan list 查看

Laravel框架定时任务2种实现方式示例

3、然后修改: laravel\app\Console\Kernel.php 文件

<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
 protected $commands = [
  // 参考手册 新加
  \App\Console\Commands\test::class,
 ];
 // 定义应用的命令调度
 protected function schedule(Schedule $schedule)
 {
   // 新加 每分钟执行一次
  $schedule->command('test:insert')->everyMinute();
 }
 protected function commands()
 {
  $this->load(__DIR__.'/Commands');
  require base_path('routes/console.php');
 }
}

4、启用计划任务:在服务器中加入到计划任务 crontab -e

注意这里的 path 是你的laravel项目根目录的 绝对路径!, 然后加上后面的 artisan 到结尾的字符串

* * * * * php /path/artisan schedule:run >> /dev/null 2>&1
* * * * * php /code/src/laravel/artisan schedule:run >> /dev/null 2>&1

5、打开日志文件查看

laravel\storage\logs\laravel.log

第二种

使用 shell脚本执行

因为 php artisan list 可以查看到 执行指令 test:insert

所以可以考虑用 .sh 脚本执行,还是类似上面 crontab -e编写

1、先编写 .sh 脚本 laravel/test.sh 放在项目某个位置,文件内写入

php artisan test:insert

上面指令在命令行手动每执行一次就可以触发一次编写的程序,相当于给 laravel.log 写入一次 test

2、使用 crontab -e 编写 执行 第一步写的 test.sh 脚本

* * * * * laravel/test.sh

以上两种均可看到 laravel.log 日志

Laravel框架定时任务2种实现方式示例

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

PHP 相关文章推荐
PHP 和 XML: 使用expat函数(一)
Oct 09 PHP
php获取通过http协议post提交过来xml数据及解析xml
Dec 16 PHP
php curl的深入解析
Jun 02 PHP
ThinkPHP的截取字符串函数无法显示省略号的解决方法
Jun 25 PHP
php正则替换处理HTML页面的方法
Jun 17 PHP
php身份证号码检查类实例
Jun 18 PHP
php简单生成随机数的方法
Jul 30 PHP
php实现json编码的方法
Jul 30 PHP
解读PHP的Yii框架中请求与响应的处理流程
Mar 17 PHP
php 魔术常量详解及实例代码
Dec 04 PHP
如何解决PHP获取不到SESSION信息之一般情况
Oct 10 PHP
PHP实现长轮询消息实时推送功能代码实例讲解
Feb 26 PHP
PHP单例模式模拟Java Bean实现方法示例
Dec 07 #PHP
thinkPHP框架实现的简单计算器示例
Dec 07 #PHP
PHP实现的简单留言板功能示例【基于thinkPHP框架】
Dec 07 #PHP
laravel5使用freetds连接sql server的方法
Dec 07 #PHP
php多进程模拟并发事务产生的问题小结
Dec 07 #PHP
Ubuntu 16.04中Laravel5.4升级到5.6的步骤
Dec 07 #PHP
PHP ajax+jQuery 实现批量删除功能实例代码小结
Dec 06 #PHP
You might like
PHP 创建文件(文件夹)以及目录操作代码
2010/03/04 PHP
php+ajax实现异步上传文件或图片功能
2017/07/18 PHP
kindeditor 加入七牛云上传的实例讲解
2017/11/12 PHP
详解PHP队列的实现
2019/03/14 PHP
php使用Swoole实现毫秒级定时任务的方法
2020/09/04 PHP
Javascript中的变量使用说明
2010/05/18 Javascript
js中数组Array的一些常用方法总结
2013/08/12 Javascript
使用canvas实现仿新浪微博头像截取上传功能
2015/09/02 Javascript
js图片轮播效果实现代码
2020/04/18 Javascript
js判断空对象的实例(超简单)
2016/07/26 Javascript
原生js实现秒表计时器功能
2017/02/16 Javascript
jQuery扩展_动力节点Java学院整理
2017/07/05 jQuery
Angular之toDoList的实现代码示例
2017/12/02 Javascript
原生js实现3D轮播图
2020/03/21 Javascript
Python 创建子进程模块subprocess详解
2015/04/08 Python
Python实现二叉搜索树
2016/02/03 Python
使用Python3 编写简单信用卡管理程序
2016/12/21 Python
matplotlib作图添加表格实例代码
2018/01/23 Python
python如何修改装饰器中参数
2018/03/20 Python
浅析Python装饰器以及装饰器模式
2018/05/28 Python
PyCharm 设置SciView工具窗口的方法
2019/01/15 Python
python pip源配置,pip配置文件存放位置的方法
2019/07/12 Python
Pytorch卷积层手动初始化权值的实例
2019/08/17 Python
python要安装在哪个盘
2020/06/15 Python
提高python代码运行效率的一些建议
2020/09/29 Python
python上下文管理器异常问题解决方法
2021/02/07 Python
CSS3 border-radius圆角的实现方法及用法详解
2020/09/14 HTML / CSS
印尼购物网站:iLOTTE
2019/10/16 全球购物
英国在线药房和在线医生:LloydsPharmacy
2019/10/21 全球购物
PHP面试题附答案
2015/11/28 面试题
会计职业生涯规划范文
2014/01/04 职场文书
党的群众路线教育实践方案
2014/05/11 职场文书
企业总经理助理岗位职责
2014/09/12 职场文书
2015年全国爱耳日活动总结
2015/02/27 职场文书
运动会通讯稿200字
2015/07/20 职场文书
《小摄影师》教学反思
2016/02/18 职场文书