这里我是用tp6进行测试的:适合做本地项目

博客参考::

https://www.thinkphp.cn/topic/64455.html

1:composer  安装workman插件

composer require workerman/workerman

2:创建 Timer 命令

php think make:command Timer

3:此时app/command就会生成一个php文件,我们再这里进行写定时任务代码即可;

复制代码;

class SelfTimer extends Command
{
protected $timer;
protected $interval = 10; protected function configure()
{
// 指令配置
$this->setName('timer')
->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
->addOption('i', null, Option::VALUE_OPTIONAL, '多长时间执行一次')
->setDescription('开启/关闭/重启 定时任务');
} protected function init(Input $input, Output $output)
{
global $argv;
if ($input->hasOption('i'))
$this->interval = floatval($input->getOption('i'));
$argv[1] = $input->getArgument('status') ?: 'start';
if ($input->hasOption('d')) {
$argv[2] = '-d';
} else {
unset($argv[2]);
}
} protected function execute(Input $input, Output $output)
{ $this->init($input, $output);
//创建定时器任务
$task = new Worker();
$task->count = 1;
$task->onWorkerStart = [$this, 'start'];
$task->runAll();
} public function stop()
{
//手动暂停定时器
\Workerman\Lib\Timer::del($this->timer);
} public function start()
{
$last = time();
$task = [6 => $last, 10 => $last, 30 => $last, 60 => $last, 180 => $last, 300 => $last];
$this->timer = \Workerman\Lib\Timer::add($this->interval, function () use (&$task) {
//可以在这里写你想要实现的功能
$data= "这是测试任务";
$path=public_path().rand(1,333).'.'.'html';
file_put_contents($path,$data);
});
}
}

我这里的定时任务是在文件public 下随机生成一个.txr文件进行测试

4:注册 Timer 命令

修改config/console.php 文件

<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
// 指令定义
'commands' => [
'timer'=>\app\command\SelfTimer::class
],
];

5:启动定时器

  1. php think timer start
查看执行的代码:

效果图:

6:关闭定时器

  1. php think timer stop

tp 实现定时任务的更多相关文章

  1. tp定时任务,传参问题

    <?phpnamespace app\command; use think\console\Command;use think\console\Input;use think\console\i ...

  2. Centos创建定时任务和开机启动运行脚本

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArYAAADlCAIAAAAp5CPLAAAgAElEQVR4nNS8d3cj15nuW/wq91vc8d ...

  3. PHP定时任务Crontab结合CLI模式详解

    从版本 4.3.0 开始,PHP 提供了一种新类型的 CLI SAPI(Server Application Programming Interface,服务端应用编程端口)支持,名为 CLI,意为 ...

  4. Java定时任务的常用实现

    Java的定时任务有以下几种常用的实现方式: 1)Timer 2)ScheduledThreadPoolExecutor 3)Spring中集成Cron Quartz 接下来依次介绍这几类具体实现的方 ...

  5. [转]Java实现定时任务的三种方法

    在应用里经常都有用到在后台跑定时任务的需求.举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据.文件等.在本文里,我会给大家介绍3种不同的实现方法: 普通thread实现 Tim ...

  6. 使用python crontab设置linux定时任务

    熟悉linux的朋友应该知道在linux中可以使用crontab设置定时任务.可以通过命令crontab -e编写任务.当然也可以直接写配置文件设置任务. 但是有时候希望通过脚本自动设置,比如我们应用 ...

  7. C#定时任务组件之FluentScheduler

    FluentScheduler是.NET开源处理定时任务组件 1.任务的创建注册 public static void TaskActionByMinutes(Action action, int c ...

  8. 浅谈 linux 例行性工作 crontab (linux定时任务)

    定时任务大家都挺说过,就好比你手机上的闹钟,到了指定的时候就会响起. 今天在对redis缓存进行定时储存时又操作了一把,发现一些细节,写的不好.大家就将就看吧, 首先 简单介绍一下linux 例行性工 ...

  9. SpringMVC中定时任务配置

    在项目中使用定时任务是常有的事,比如每天定时进行数据同步或者备份等等. 以前在从事C语言开发的时候,定时任务都是通过写个shell脚本,然后添加到linux定时任务中进行调度的. 现在使用Spring ...

随机推荐

  1. NumPy 数组学习手册·翻译完成

    原文:Learning NumPy Array 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. 在线阅读 ApacheCN 面试求职交流群 ...

  2. 使用Java开发桌面即时通讯程序遇到的问题

    项目:https://www.lking.top/?p=87 1. JPanel面板绘制背景图片问题. 参考大佬:https://www.jb51.net/article/101516.htm 本项目 ...

  3. action标签中method={1}怎么理解

    其实用到method={数字}的时候,相应的前面的action是要出现*通配符来搭配的.比如一个小例子:<action name="user_*" class="U ...

  4. JS 获取JSON返回的时间值转换为通常格式展示

    var date = new Date(parseInt(数据源.slice(6)));   //获取到时间  年月日时分秒 var result = date.getFullYear() + '/' ...

  5. 帆软报表(finereport)点击事件对话框打开

    点击事件对话框打开iframe var iframe = $("<iframe id='001' name='001' width='100%' height='100%' scrol ...

  6. 03 前端基础之JavaScript

    目录 前端基础之JavaScript JavaScript JavaScript注释 变量与常量 基本数据类型 number类型 string类型 boolean类型 null与undefined类型 ...

  7. Solution -「CF 923E」Perpetual Subtraction

    \(\mathcal{Description}\)   Link.   有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...

  8. Solution -「CF 555E」Case of Computer Network

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个点 \(m\) 条边的无向图,判断是否有给每条边定向的方案,使得 \(q\) 组有序点对 \((s,t)\) ...

  9. Solution -「ARC 104C」Fair Elevator

    \(\mathcal{Description}\)   Link.   数轴从 \(1\sim 2n\) 的整点上有 \(n\) 个闭区间.你只知道每个区间的部分信息(可能不知道左或右端点,或者都不知 ...

  10. Spring Boot数据访问之Druid连接池的配置

    在Spring Boot数据访问之数据源自动配置 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中数据源连接池方式缺省(type)情况下默认使用HikariCP,那不缺省如何配置呢?我们 ...