laravel在windows环境下设置定时任务进行任务调度,laravel schedule定时任务

1 篇文章 0 订阅
1 篇文章 0 订阅

在Windows下进行laravel的任务调度。
1.创建任务代码:

php artisan make:command LogRecord

在App\Console\Commands目录下会生成一个RecordLog.php的文件,内容如下:

<?php

namespace App\Console\Commands;

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

class RecordLog extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Log:record'; //名字随便写,后续任务会用到

    /**
     * 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()
    {
        //任务的具体内容
        //例如:记录信息到log
        Log::info(date('Y-m-d H:i:s').'使用command()方法实现定时任务');
    }
}

然后在App/Console/Kernel.php文件中写入调度命令,如下

<?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 = [
        //注册对应的command类  刚刚创建的命令类
        \App\Console\Commands\RecordLog::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // command('Log:record')里面填写之前在RecordLog文件里面的$signature参数
        // everyMinute()此为每分钟执行任务
        $schedule->command('Log:record')->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

然后在命令行中运行如下代码测试:

php artisan schedule:run

在storage/logs目录下找到当天日期的log文件,查看是否成功写入log内容。
2.创建定时任务
在任何一个你觉得合适的地方创建一个txt,然后打开,写入如下代码:

cd /d F:\\wamp\www\zhiwu\
php artisan schedule:run 1>> NUL 2>&1

代码解释:第一句:是通过切换盘符的方式进入F:\wamp\www\zhiwu\ 的根目录,/d:以切换盘符的方式进入
第二句:这个就很好理解了,就是laravel的任务调度命令了。
然后保存,并且将txt文档重命名为:zhiwu.bat。
然后win+R 输入 命令:taskschd.msc
进行创建基础任务。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
这一步要注意,这里的程序脚本是之前编写的.bat文件,
在这里插入图片描述
这里是否勾选都无所谓,没有勾选的,在任务库列表双击杠杠创建的任务即可。
在这里插入图片描述
这里选择新建,在这里插入图片描述
其中4的位置,可以自己手动输入,但是切记,数字和汉字中间有个空格。(为了节省调试时间,建议设置成1分钟)
操作完成之后确定,就可以了。
然后在任务库列表中可以进行运行了。
但这里有个问题,就是每次激活任务的时候,会有一个黑色框一闪而过,此时,可以利用vbs文件进行设置一下。
在任何位置(最好是和上面的.bat文件同目录)创建一个txt文件,输入命令:

Set ws = CreateObject("Wscript.Shell")   
ws.run "cmd /c G:\Desktop\zhiwu.bat",vbhide

将命令中的.bat文件的绝对路径更换成之前创建的.bat文件,然后保存,修改为.vbs后缀的文件,
再回到定时任务列表,双击创建的任务,
在这里插入图片描述
选择编辑,
在这里插入图片描述
脚本选择创建的.vbs文件,
然后就可以了。
到此,windows下进行laravel任务调度就OK了

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值