laravel 中定時任務 創建

問題: linux下設置任務需要每個任務都配置一條命令,不方便管理。 laravel框架提供了, 統一管理方法。

方法: 1.在app/Console 目錄下創建 Commands/文件名 ,拿test舉例吧
代碼如下

  <?php
	namespace App\Console\Commands;

	use Illuminate\Console\Command;
	use Illuminate\Support\Facades\Log;

	class TestConsole extends Command
	{

	    protected $signature = 'testconsole';

	    /**
	     * The console command description.
	     *
	     * @var string
	     */
	    protected $description = '這是一個測試artisan的描述';

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

	    /**
	     * Execute the console command.
	     *
	     * @return mixed
	     */
	    public function handle()
	    {
	        Log::info('這是我寫的log');
	    }


	}


?>
2. 1>.在Console/Kernel.php 中 $commands中加入 test文件
				protected $commands = [
			        Commands\TestConsole::class
			        //
			    ];
2>.在schedule方法里加入 命令 
  			 protected function schedule(Schedule $schedule) {
			        $schedule->command('testconsole')->everyMinute();
			    }
3>. 設置laravel統一crontab。
		       crontab -e 
		     
		     * * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

		     		php爲php命令所在的路徑  例如: /aws/server/php/bin/php 

		     		path-to-your-project爲你的laravel項目路徑  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章