首页 > 其他分享 >laravel:定时任务(10.27.0)

laravel:定时任务(10.27.0)

时间:2023-10-22 10:44:41浏览次数:50  
标签:laravel 10 Console 10.27 cron command 定时 php

一,相关的文档:

https://learnku.com/docs/laravel/10.x/scheduling/14875

二,php代码:

1,创建command:

liuhongdi@lhdpc:/data/laravel/dignews$ php artisan make:command OrderStatus

   INFO  Console command [app/Console/Commands/OrderStatus.php] created successfully.

2,编写代码:

app/Console/Commands/OrderStatus.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <?php   namespace App\Console\Commands;   use Illuminate\Console\Command; use Illuminate\Support\Facades\Log;     class OrderStatus extends Command {     /**      * The name and signature of the console command.      *      * @var string      */     protected $signature = 'app:order-status';    //此处作为命令的名称       /**      * The console command description.      *      * @var string      */     protected $description = 'Command description';       /**      * Execute the console command.      */     public function handle()     {         //此处添加定时执行的业务逻辑         Log::channel('business')->info("定时程序运行一次");     } }

3,注册定时任务到app/Console/Kernel.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <?php   namespace App\Console;   use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel;   class Kernel extends ConsoleKernel {     /**      * Define the application's command schedule.      */     protected function schedule(Schedule $schedule): void     {         //注册定时运行的程序,每分钟执行一次         $schedule->command('app:order-status')->everyMinute();     }       /**      * Register the commands for the application.      */     protected function commands(): void     {         $this->load(__DIR__.'/Commands');           require base_path('routes/console.php');     } }

三,添加程序到linux的cron服务

在crontab中添加定时任务

root@lhdpc:/data/laravel/dignews# crontab -l
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
# * * * * * cd /data/laravel/dignews && php artisan schedule:run >> /dev/null 2>&1
* * * * * cd /data/laravel/dignews && /usr/local/soft/php8/bin/php artisan schedule:run >> /dev/null 2>&1

查看cron服务的状态:

root@lhdpc:/data/laravel/dignews# systemctl status cron.service 
● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-10-16 18:02:37 CST; 3 days ago
       Docs: man:cron(8)
   Main PID: 634 (cron)
      Tasks: 1 (limit: 4582)
     Memory: 13.3M
        CPU: 41.340s
     CGroup: /system.slice/cron.service
             └─634 /usr/sbin/cron -f -P
...

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/19/laravel-ding-shi-ren-wu-10-27/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]

四,测试效果

root@lhdpc:/data/laravel/dignews# tail -100 /data/laravel/logs/image-2023-10-19.log
[2023-10-19 07:04:01] local.INFO: 定时程序运行一次  
[2023-10-19 07:05:01] local.INFO: 定时程序运行一次  
[2023-10-19 07:06:02] local.INFO: 定时程序运行一次  
[2023-10-19 07:07:01] local.INFO: 定时程序运行一次
...

五,查看laravel的版本:

liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0

标签:laravel,10,Console,10.27,cron,command,定时,php
From: https://www.cnblogs.com/architectforest/p/17780045.html

相关文章

  • laravel:使用tinker(10.27.0)
    一,启动与退出:liuhongdi@lhdpc:/data/laravel/dignews$phpartisantinkerPsyShellv0.11.22(PHP8.1.1—cli)byJustinHileman>exit   INFO  Goodbye.二,查询数据liuhongdi@lhdpc:/data/laravel/dignews$phpartisantinkerPsyShellv0.11.22(PHP8.1.......
  • 4、定时器模块
    定时器可以执行计时和计数的任务。Systic定时器也叫做滴答定时器,是一个24位的倒计数定时器,计到0时,将从RELOAD寄存器中自动重装载定时初值。只要不把它在SysTick控制及状态寄存器中的使能位清除,就永不停息,即使在睡眠模式下也能工作。 寄存器方面:控制寄存器:CTRL ......
  • esp32笔记[8]-rust的定时器中断点灯
    摘要使用rust开发esp32c3实现定时器中断点亮led灯.超链接esp32笔记[7]-使用rust+zig开发入门平台信息esp32c3rustMutex锁usecore::cell::RefCell;usecritical_section::Mutex;//no-std库专用的Mutex我们首先注意到的是静态变量BUTTON。我们需要它,因为在中断处理程......
  • php js + laravel + mysql开发的手术麻醉临床信息系统源码
    手术麻醉临床信息系统有着完善的临床业务功能,能够涵盖整个围术期的工作,能够采集、汇总、存储、处理、展现所有的临床诊疗资料。通过该系统的实施,能够规范麻醉科的工作流程,实现麻醉手术过程的信息数字化,自动生成麻醉的各种医疗文书,完成共享HIS、LIS、PACS和EMR等手术患者信息,从而提......
  • Python定时任务框架APScheduler
    Python定时任务框架APSchedulerPython定时任务框架APScheduler详解-CSDN博客python定时任务最强框架APScheduler详细教程-知乎(zhihu.com) 课程详情接口思路一:直接在之前写好的查询所有课程的视图类上,配置一个类即可classCourseView(GenericViewSet,CommonListModelM......
  • laravel:开启/关闭调试模式(10.27.0)
    一,文档地址:https://learnku.com/docs/laravel/10.x/configuration/14836#701998二,设置1,.env中关于调试的默认值:APP_DEBUG=true2,关闭调试APP_DEBUG=false说明:刘宏缔的架构森林—专注it技术的博客,网站:https://blog.imgtouch.com原文: https://blog.imgtouch.com/ind......
  • laravel:访问redis(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/redis/14887二,php代码1,配置.env使用默认的设置:REDIS_HOST=127.0.0.1REDIS_PASSWORD=nullREDIS_PORT=63792,controller中引用:12345678910111213141516171819202122232425<?ph......
  • laravel:关闭默认首页(10.27.0)
    一,php代码修改routes/web.php原代码:Route::get('/',function(){    returnview('welcome');});修改后Route::get('/',function(){    $appName=env('APP_NAME');    return['code'=>0,'msg'=>&#......
  • laravel:部署到nginx服务器(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/deployment/14840二,配置nginx1,站点文件server{listen80;#listen[::]:80;server_namedig.lhdtest.com;root/webdata/site/dig/public;add_headerX-Frame-Options"SAMEORIGIN";a......
  • celery包结构、celery延迟任务和定时任务、django中使用celery、接口缓存、双写一致性
    celery包结构project├──celery_task#celery包│├──__init__.py#包文件│├──celery.py#celery连接和配置相关文件,且名字必须叫celery.py│└──tasks.py#所有任务函数├──add_task.py#添加任务......