Windows系统定时任务

1. 运行环境

windows 10,小皮,php 7.3

1). 当前使用的 Laravel 版本?

laravel 8

2. 问题描述?

想要1秒钟GET一个接口,把获取的数据存到数据库,但Windows系统自带的任务计划最少1分钟,有啥更好的方法定时1秒钟执行一次吗?项目都是laravel 8的。新手小白不太会,求大佬可以指导一下,十分感谢。

Windows系统定时任务

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

可以使用wokerman解决,先下载2个包
workerman/crontab,workerman/workerman
www.workerman.net/doc/workerman/co... 文档

composer require workerman/workerman
composer require workerman/crontab

在laravel中使用
执行命令

php artisan make:command crontab

在生成的文件中

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Workerman\Worker;

class Crontab extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'crontab';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'crontab';

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

    /**
     * Execute the console command.
     *
     */
    public function handle()
    {
        $worker = new Worker();
        $worker->onWorkerStart = [$this, 'onWorkerStart'];
        $worker->runAll();
    }

    public function onWorkerStart()
    {
        // 每秒执行
        new \Workerman\Crontab\Crontab('* * * * * *', function(){
            Artisan::call('需要执行的任务');
        });
    }
}

在命令行工具,项目目录下执行

php artisan crontab

如果想在后台运行,搜索pm2自己安装试试

5个月前 评论
SiuhoY (楼主) 5个月前
ononl (作者) 5个月前
SiuhoY (楼主) 5个月前
讨论数量: 10

简单点写个死循环完事让他跑着就行。或者用workerman的crontab 也可以实现 github.com/walkor/crontab

5个月前 评论

使用workerman,写个定时任务,可以搞

5个月前 评论

wsl或者pm2+while(true),另外我记得有个包可以在命令行下模拟schedule,配合pm2也能实现

5个月前 评论

可以使用wokerman解决,先下载2个包
workerman/crontab,workerman/workerman
www.workerman.net/doc/workerman/co... 文档

composer require workerman/workerman
composer require workerman/crontab

在laravel中使用
执行命令

php artisan make:command crontab

在生成的文件中

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Workerman\Worker;

class Crontab extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'crontab';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'crontab';

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

    /**
     * Execute the console command.
     *
     */
    public function handle()
    {
        $worker = new Worker();
        $worker->onWorkerStart = [$this, 'onWorkerStart'];
        $worker->runAll();
    }

    public function onWorkerStart()
    {
        // 每秒执行
        new \Workerman\Crontab\Crontab('* * * * * *', function(){
            Artisan::call('需要执行的任务');
        });
    }
}

在命令行工具,项目目录下执行

php artisan crontab

如果想在后台运行,搜索pm2自己安装试试

5个月前 评论
SiuhoY (楼主) 5个月前
ononl (作者) 5个月前
SiuhoY (楼主) 5个月前

计划任务 doak-cron

win下可以用vbs自启

5个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!