Windows系统定时任务

1. 运行环境#

windows 10,小皮,php 7.3

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

laravel 8

2. 问题描述?#

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

Windows系统定时任务

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

可以使用 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 自己安装试试

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

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

6个月前 评论

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

6个月前 评论

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

6个月前 评论

可以使用 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 自己安装试试

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

计划任务 doak-cron

win 下可以用 vbs 自启

6个月前 评论