• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-01) 42次浏览 已收录 0个评论

这篇文章主要介绍了Laravel框架定时任务2种实现方式,结合实例形式较为详细的分析了Laravel框架定时任务相关实现方法及操作注意事项,需要的朋友可以参考下

本文实例讲述了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 $desc<i style="color:transparent">来源gaodai$ma#com搞$$代**码网</i>ription = '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 文件

 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相关内容感兴趣的读者可查看本站专题:《》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

以上就是Laravel框架定时任务2种实现方式示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Laravel框架定时任务2种实现方式示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址