Laravel 定時任務


一 、創建Command文件

php artisan make:console RefreshToken

 

創建app/Console/Commands 下創建RefreshToken.php

取一個名字

protected $signature = 'refreshToken';

 

添加描述

protected $description = 'Refresh Api Token';

 

 在 handle() 方法中實現功能

public function handle()
{
    Log::info('Token Refreshed');
}

 

二、在Kernel.php 文件中注冊

    //Commands\Inspire::class,
         Commands\RefreshToken::class,
    ];
protected function schedule(Schedule $schedule)
    {
         $schedule->command('refreshToken')
                  ->everyMinute();
    }

 

三、新建cron.txt文件

文件內容:

* * * * * php /home/w/code/BTLIS/artisan schedule:run >> /dev/null 2>&1

 

四、用命令開啟任務

在終端中項目目錄下(Linux 環境)
告訴crontab 文件名: 

crontab cron.txt


開始定時任務

crontab -l

 

或許之后你要結束任務 

crontab -r

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM