本文实例讲述了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 查看

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 日志

您可能感兴趣的文章:

文章同步发布: https://www.geek-share.com/detail/2755357086.html

Laravel框架定时任务2种实现方式示例的更多相关文章

  1. EF框架的三种工作方式

    EF框架step by step(1)—Database-First EF框架step by step(2)—Model-First EF框架step by step(3)—Code-First 通过 ...

  2. IndexFlatL2、IndexIVFFlat、IndexIVFPQ三种索引方式示例

    上文针对Faiss安装和一些原理做了简单说明,本文针对标题所列三种索引方式进行编码验证. 首先生成数据集,这里采用100万条数据,每条50维,生成数据做本地化保存,代码如下: import numpy ...

  3. Vue框架的两种使用方式

    1.单页面应用:使用Vue CLI工具生成脚手架,这是最常见的使用方式,简单用模板生成一个HelloWorld Demo,可以学习Vue的SPA项目结构 2.传统多页面应用:通过script引入Vue ...

  4. Windows下Yii2框架的两种安装方式及升级最新版

    第一种:归档文件形式安装(适合于没有安装composer的机器) 进入下载页https://www.yiiframework.com/download,选择下载第一个 下载完成之后是一个压缩包,解压文 ...

  5. 使用 Composer 安装 Laravel 框架

    前言: 1. Composer 安装 Laravel 有两种方式: 第一种是通过 Composer 的 create-project 命令安装 Laravel 框架, 第二种是先通过 Composer ...

  6. SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论 SignalR 简单示例 通过三个DEMO学会SignalR的三种实现方式 SignalR推送框架两个项目永久连接通讯使用 SignalR 集线器简单实例2 用SignalR创建实时永久长连接异步网络应用程序

    SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论   异常汇总:http://www ...

  7. 使用java配置定时任务的几种配置方式及示例

    Spring定时器,主要有两种实现方式,包括Java Timer定时和Quartz定时器! 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 package ...

  8. Java 定时任务的几种实现方式

    JAVA实现定时任务的几种方式 @(JAVA)[spring|quartz|定时器]  近期项目开发中需要动态的添加定时任务,比如在某个活动结束时,自动生成获奖名单,导出excel等,此类任务由于活动 ...

  9. spring定时任务的几种实现方式

    Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将 ...

随机推荐

  1. Win10系统创建关机快捷方式和快捷键的方法,实现一键关机

    想不想关机变得更加简单?在Win10中总有些朋友想要快速的操作体验,所以关机这个经常使用的功能也被设置的更简单,下面小编要分享的是“Win10系统创建关机快捷方式和快捷键的方法”. 1.在win10下 ...

  2. kafka概要设计

    Kafka核心功能 即:高性能的消息发送与高性能的消息消费 下载安装包后即可启动Kafka服务器,但是此前需要首先启动Zookeeper服务器,Zookeeper是为Kafka提供协调服务的工具,Ka ...

  3. angularJs的$scope.$apply

    <!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" ...

  4. Failed to read Class-Path attribute from manifest of jar file:/XXX问题

    java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file:/XXX ...

  5. 语法规范:BNF与ABNF 巴斯克范式

    语法规范:BNF与ABNF 巴斯克范式 BNF  巴科斯范式(BNF: Backus-Naur Form 的缩写)是由 John Backus 和 Peter Naur 首先引入的用来描述计算机语言语 ...

  6. LeetCode: Word Ladder II [127]

    [题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  7. Maven经常使用命令

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/UP19910522/article/details/25985021 Maven库: http:// ...

  8. 【jQuery】jQuery与Ajax的应用

    1.demo1 <script language="javascript" type="text/javascript"> //通过这个函数来异步获 ...

  9. Ubuntu 14.04 系统安装后无法上网的问题(eth0识别不出)

    Ubuntu 14.04 1.网口处网线状态等正常跳动 2.ifconfig 指令查询不到网卡信息 说明缺少了网卡驱动. 使用 lspci 指令查看系统中所有的驱动信息,找到 Ethernet Con ...

  10. arcgis js api proxy java 版本配置

    <?xml version="1.0" encoding="utf-8" ?> <ProxyConfig allowedReferers=&q ...