Lavarel定时任务的使用

系统为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 命令

  1. 打开任务计划程序;在 Windows 搜索框中输入 “任务计划程序” 并打开该程序。
  2. 创建新任务;在任务计划程序中,选择 “创建任务”,然后按照指示设置一个名称和描述。
  3. 设置触发器;选择 “启动该任务的触发器”,然后选择 “新建”。在触发器设置中,选择 “一次”,并且设置 “重复间隔” 为 1 分钟。
  4. 设置操作;在操作选项卡中,选择 “新建”,并输入以下信息:
  5. 操作:启动程序;程序/脚本:php.exe 的完整路径(比如 D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe) ;参数:artisan schedule:run ;起始于:你的 Laravel 项目的路径(D:\Users\Desktop\your-project-name);
  6. 完成设置;完成以上设置后,保存任务并关闭任务计划程序。

你可能感兴趣的:(php,lavarel,后端,php)