tp 实现定时任务
这里我是用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:启动定时器
- php think timer start
效果图:

6:关闭定时器
- php think timer stop
tp 实现定时任务的更多相关文章
- tp定时任务,传参问题
<?phpnamespace app\command; use think\console\Command;use think\console\Input;use think\console\i ...
- Centos创建定时任务和开机启动运行脚本
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArYAAADlCAIAAAAp5CPLAAAgAElEQVR4nNS8d3cj15nuW/wq91vc8d ...
- PHP定时任务Crontab结合CLI模式详解
从版本 4.3.0 开始,PHP 提供了一种新类型的 CLI SAPI(Server Application Programming Interface,服务端应用编程端口)支持,名为 CLI,意为 ...
- Java定时任务的常用实现
Java的定时任务有以下几种常用的实现方式: 1)Timer 2)ScheduledThreadPoolExecutor 3)Spring中集成Cron Quartz 接下来依次介绍这几类具体实现的方 ...
- [转]Java实现定时任务的三种方法
在应用里经常都有用到在后台跑定时任务的需求.举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据.文件等.在本文里,我会给大家介绍3种不同的实现方法: 普通thread实现 Tim ...
- 使用python crontab设置linux定时任务
熟悉linux的朋友应该知道在linux中可以使用crontab设置定时任务.可以通过命令crontab -e编写任务.当然也可以直接写配置文件设置任务. 但是有时候希望通过脚本自动设置,比如我们应用 ...
- C#定时任务组件之FluentScheduler
FluentScheduler是.NET开源处理定时任务组件 1.任务的创建注册 public static void TaskActionByMinutes(Action action, int c ...
- 浅谈 linux 例行性工作 crontab (linux定时任务)
定时任务大家都挺说过,就好比你手机上的闹钟,到了指定的时候就会响起. 今天在对redis缓存进行定时储存时又操作了一把,发现一些细节,写的不好.大家就将就看吧, 首先 简单介绍一下linux 例行性工 ...
- SpringMVC中定时任务配置
在项目中使用定时任务是常有的事,比如每天定时进行数据同步或者备份等等. 以前在从事C语言开发的时候,定时任务都是通过写个shell脚本,然后添加到linux定时任务中进行调度的. 现在使用Spring ...
随机推荐
- ArrayList和LinkList的区别
底层实现区别 ArrayList 底层实现就是数组,且ArrayList实现了RandomAccess,表示它能快速随机访问存储的元素,通过下标 index 访问,只是我们需要用 get() 方法的形 ...
- 深入解析HashMap、HashTable (转)
集合类之番外篇:深入解析HashMap.HashTable Java集合类是个非常重要的知识点,HashMap.HashTable.ConcurrentHashMap等算是集合类中的重点,可谓&quo ...
- 总结一下Mac快捷键的图形符号
Mac中主要有四个修饰键,分别是Command,Control,Option和Shift.这四个键分别有自己的图案,他们经常出现在Mac应用程序中的菜单栏里,方便你随时学习新的快捷键.
- vi/vim 设置.vimrc(/etc/vim | $HOME)
转载请注明来源:https://www.cnblogs.com/hookjc/ "====================================================== ...
- axios ajax fetch 区别以及优缺点
将jQuery的ajax.axios和fetch做个简单的比较,所谓仁者见仁智者见智,最终使用哪个还是自行斟酌 1.jQuery ajax $.ajax({ type: 'POST', url: ur ...
- linux 下 ifcfg-eth0 配置
网络接口配置文件[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0# Intel Corporation 82545EM ...
- 【转载收藏】使用Jacoco远程统计tomcat服务的代码覆盖率
在做软件开发的时候经常会遇到做各种测试,这里介绍一种本人遇到的:代码覆盖率测试, 这个测试非常实用,能发现许多无效的模块和代码.强烈推荐!!!!! 网上好多资料都不全,而且没有详细的配置流程 本文将简 ...
- LaunchScreen&LaunchImage
优先级:LaunchScreen > LaunchImage 在xcode配置了,不起作用 1.清空xcode缓存 2.直接删掉程序 重新运行 如果是通过LaunchImage设置启动界面,那么 ...
- js实现网页中英文翻译
1,html 2,metrics.js 3,需要 http://www.microsoftTranslator.com/ajax/v3/WidgetV3.ashx?siteData=ueOIGRSKk ...
- iOS 启动画面 App图标尺寸 项目上架必须Icon设置
1.1AppIcon图标尺寸如下: 说明:AppIcon (6张) AppStore Icon (1张)(png格式) AppStore Icon --- 1024x1024(必须) 1.2启动画 ...