系统为window
执行命令(执行一次命令只会根据当前时间运行一次定时任务)
php artisan schedule:run
创建一个任务类(在Jobs文件夹下面)
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class YourJobName implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info('执行定时任务');
}
}
注册任务:在app/Console/Kernel.php文件中进行注册
protected function schedule(Schedule $schedule)
{
$schedule->job(new YourJobName)->everyMinute();//每分钟执行一次
}
在 Windows 上配置定时任务以每分钟执行 php artisan schedule:run
命令
artisan schedule:run
;起始于:你的 Laravel 项目的路径(D:\Users\Desktop\your-project-name);