这里我是用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. 关于https域名下的页面iframe嵌套http页面的问题

    业务场景:在一个https域名下用iframe嵌套一个http域名的页面,会直接报错的,报错信息如下: 这段话的意思是:http域名的页面是通过https域名页面加载的,在一个安全的页面不允许加载一个 ...

  2. 「BalkanOI 2018 Day1」Election

    「BalkanOI 2018 Day1」Election 记C为1,T为-1,\(sum[i]\)为\(i\)点的前缀和. 对于询问\([l,r]\),分两步计算答案. 要求所有点的\(sum[i]- ...

  3. Linux之shell入门

    一.编写规范 代码规范: #!/bin/bash [指定告知系统当前这个脚本要使用的shell解释器] shell相关指令 文件命名规范: 文件名.sh .sh是linux下bash shell 的默 ...

  4. 通过Xib加载控制器的View

    1.创建窗口self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];2.设置窗口根控制器2.1从XIB当 ...

  5. shell脚本一键部署——Redis(直接复制执行)亲测100% 有效

    首先所需软件包拖进目录,然后编辑一个脚本,复制一下脚本,source执行一下即可 #!/bin/bash echo -e "\033[31m =====正在验证当前为仅主机还是NAT模式== ...

  6. DNS域名解析之反向解析and主从域名服务器 (今天大小便正常,未来可期)

    DNS解析之反向解析和域名主从服务器 反向解析:根据IP地址查找对应的域名 yum -y install bind 安装软件包 查看需要修改的配置文件所在路径 rpm -qc bind 查询bind软 ...

  7. Scala概述及环境配置

    一.概述 1.1 为什么要学Scala? Spark是新一代的内存级大数据的计算框架,是大数据学习的重要内容.随着Spark的兴起,同时也带动了Scala的学习,因为Spark就是Scala编写的.为 ...

  8. B快速导航

    GETTING STARTED If you are new to Selenium, we have a few resources that can help you get up to spee ...

  9. [LeetCode]9.回文数(Java)

    原题地址: palindrome-number 题目描述: 给你一个整数 x ,如果 x 是一个回文整数,返回 true :否则,返回 false . 回文数是指正序(从左向右)和倒序(从右向左)读都 ...

  10. kubernetes之数据管理

    volume emptyDir [machangwei@mcwk8s-master ~]$ kubectl apply -f mcwVolume1.yml #部署emptydir pod/produc ...