1.寫任務 app/Console/Commands
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Test extends Command
{
/**
* 簽名
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test{type}{--id=}{--date=}';
/**
* 說明
* The console command description.
*
* @var string
*/
protected $description = '任務調度測試';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$type = $this->argument('type'); // 無參數名參數用這個接
$id = $this->option('id'); // 有參數名用這個接
$date = $this->option('date'); // 有參數名用這個接
$response = [
'type' => $type,
'id' => $id,
'date' => $date
];
info(date('Y-m-d H:i:s'),$response);
// 以下可以進行定時任務,執行你想執行的操作
}
}
2.調度任務 app/Console/Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* 命令 引入要執行的命令
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\Test::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
//
$schedule->command("test 1 --id=2 --date=2020-01-08")->everyMinute();; // 每分鐘執行一次
//$schedule->command("test 1")->dailyAt('09:30'); // 每天上午9:30執行
}
}
3.服務器設置定時任務 crontab -e 寫下以下內容(路徑改爲自己的路徑)
// php 之後 artisan之前 爲你的項目根目錄的絕對路徑 其他不變
* * * * * php /Users/AarthiModoo/Desktop/E/work/lumen/artisan schedule:run >> /dev/null 2>&1