寶塔面板+laravel5.2 添加定時任務

 1、laravel添加定時任務

文檔:https://xueyuanjun.com/post/3267

1.1配置console的Kernel

 

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\SettleAccounts::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('SettleAccounts')->withoutOverlapping();


    }
}

1.2新建command腳本
新建目錄App\Console\Commands
在Commands目錄下新建SettleAccounts類

<?php

namespace App\Console\Commands;

use App\Models\Site\PunchProject;
use App\Models\Site\Regular;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class SettleAccounts extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'SettleAccounts';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '定時任務發放獎勵';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    const HAS_FAILED = 'HasFailed'; //已執行打卡失敗

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //任務邏輯 TODO

      
        $this->kernel_log('定時任務發放獎勵 ===begin====');
        $this->kernel_log('定時任務發放獎勵 $getList' . json_encode($getList));
 
        $this->kernel_log('定時任務發放獎勵 ===end====');
        // Log::info('定時任務發放獎勵 ===end====');

    }

    public function kernel_log($msg){
        //一定加$monolog這兩句,不然會打印兩份日誌
        $monolog = Log::getMonolog();
        $monolog->popHandler();
         //Log::useDailyFiles(storage_path('logs/error/test.log'));
        //  Log::useFiles(storage_path('logs/kernel_log/kernel.log'));
         Log::useDailyFiles(storage_path('logs/kernel_log/kernel.log'));
         
        //  Log::emergency("系統掛掉了");
        //  Log::alert("數據庫訪問異常");
        //  Log::critical("系統出現未知錯誤");
        //  Log::error("指定變量不存在");
        //  Log::warning("該方法已經被廢棄");
        //  Log::notice("用戶在異地登錄");
         Log::info($msg);
        //  Log::debug("調試信息");
     }


}

 

2、寶塔面板添加定時任務

 腳本內容 xxx.com爲網址域名

php /www/wwwroot/xxx.com/artisan schedule:run

注意,這裏有個坑

如果在執行腳本PHP文件裏寫了日誌,需要注意。

寶塔面板執行計劃任務生成的日誌 是 root 權限的,laravel程序生成的日誌,權限是www。
 

 

 

執行定時任務之後,再遇到其他寫日誌的地方,會報500錯誤。

 

我的解決辦法是:自定義一個文件夾存儲定時任務生成的日誌,方法已寫到上邊的文件裏

 

參考文檔:

https://www.cnblogs.com/johnson108178/p/8023972.html

https://blog.csdn.net/woqianduo/article/details/83995093

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章