当前位置 博文首页 > 缘来侍你的博客:ThinkPHP5实现定时器任务
方法一
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class Hello extends Command
{
protected function configure()
{
$this->setName('hello') //命令的名字("think" 后面的部分)
->addArgument('name', Argument::REQUIRED, "your name") //必填参数
->addArgument('last_name', Argument::OPTIONAL, 'Your last name?') //选填参数
->addOption('city', 'c', Option::VALUE_OPTIONAL, 'city name')
->setDescription('Say Hello'); // 运行 "php think list" 时的简短描述
->setHelp("This command allows you to create users..."); // 运行命令时使用 "--help" 选项时的完整命令描述
}
protected function execute(Input $input, Output $output)
{
$name = trim($input->getArgument('name'));
$name = $name ?: 'thinkphp';
$other = trim($input->getArgument('last_name'));
$other = $other ?: 'last';
if ($input->hasOption('city')) {
$city = PHP_EOL . 'From ' . $input->getOption('city');
} else {
$city = '';
}
$output->writeln("Hello," . $name .'last_name->'.$other. '!' . $city);
}
}
直接进入网站根目录,命令行执行,就可看到效果?
php think hello fujian min? --city xiamen
<?php
return [
'application\command\Hello', // 加入需要cmd运行的PHP文件
];
在项目根目录下创建shell脚本,例如crond.sh
#!/bin/sh
PATH=/usr/local/php/bin:/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin # 将php路径加入都临时变量中
# 如果你的PHP命令是已经配置了全局,可不需要上面这句
cd /home/wwwroot/域名/ # 进入项目的根目录下,保证可以运行php think的命令
php think hello xx # 执行在Hello.php设定的名称
注意:test?可执行命令是ThinkPHP自带的,可以通过?连接服务器,到/home/wwwroot/域名/?目录下,输入?php?think查询可以被执行的命令,如下:
有两种方式,效果是一样的:
1.连接到服务器,输入?crontab -e,写入:
0 0 * * * /home/wwwroot/域名/crond.sh
注意:1).0 0 * * *?是crontab的定时表达式,表示每天的0点0分执行该文件,具体详情可以访问《crontab定时写法》进行学习。
? ? ? ? ?2).可以使用crontab -l?的命令查看已登录的账户有几个定时器。
? ? ? ? ?3).可以到 /var/log/cron?文件查看日志文件,便于追踪错误。
2.连接到服务器,输入 vim /etc/crontab, 初始化内容为:
在该文件写入
0 0 * * * root /home/wwwroot/域名/crond.sh
最终的查看的结果是:
最后保存该文件
5.重启crond服务
service crond restart
如果 该命令无法重启,请使用systemctl restart crond 进行重启
----------------------------------------------------------分隔线--------------------------------------------------------------
方法二,需要修改本文的前三步,后面均一致。
1).新增Controller类,并编写相对应的方法,例如:
以下是Test Controller类,还有一个简单的test方法。
<?php namespace app\demo\controller; use think\Controller; use think\Log; class Test extends Controller { public function test(){ Log::error('start test crond demo.....'); Log::error('end test crond demo.....'); } }
访问test方法的路由:demo/test/test
2).添加shell执行文件
在项目根目录下创建shell脚本,例如crond.sh
#!/bin/sh PATH=/usr/local/php/bin:/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin # 若配置全局PHP命令,可不需要上面这句 # 1.执行 php 命令不需要到thinkphp项目的目录下 2.index.php为入口文件 3.第三个参数为需要执行方法的路由 php /home/wwwroot/域名/index.php demo/test/test
后面的步骤从本文第4步开始,就可以完成定时功能。
第二种方法符合API引用的思维,可能比较容易理解,第一种是利用thinkPHP本身的一个功能,这两种都可以,看个人习惯。
cs下一篇:没有了
最新 更多<<
缘来侍你的博客:ThinkPHP5实现定时器任务 缘来侍你的博客:宝塔提示数据库root密码不能带有特殊符号 缘来侍你的博客:MySQL IF和case的使用以及如何取两个字符串中间 baidu_39502417的博客:nrf24l01的配置流程 baidu_39502417的博客:MFC对话框编辑器 列表功能图像一览 baidu_39502417的博客:C++MFC命名规范 baidu_39502417的博客:QT 串口发送16进制 baidu_39502417的博客:整型和浮点型与QByteArray的转换----by: 历史上的今天:【历史上的今天】9 月 16 日:乔布斯的归来;苹果 英雄哪里出来:??13万字《C语言动漫对话教程(入门篇)》??(建议收 lyndon:文件大小 和 文件占用空间 大番薯:编程术语英汉对照 HashFlag的博客:Python基础 风信子的猫Redamancy的快乐星球:PRML - Chapter 02 Probability g5703129的博客:java学习笔记总结,持续更新中 晴天的专栏:怎样规划你毕业以后的人生 lj663的博客 原创内容,个人笔记,仅供参考!:SAP中导出物料 lj663的博客 原创内容,个人笔记,仅供参考!:关于作者博文的 lj663的博客 原创内容,个人笔记,仅供参考!:申明:本人博客 lj663的博客 原创内容,个人笔记,仅供参考!:关于VIP文章的 程序员springmeng:??爆肝熬夜开发了一个SpringBoot活动管理系统 相太阳的博客:CSS font-size字体大小样式属性 Jqcode:配置文件中propertyConfigurer类的使用 Jqcode:Eclipse插件使用Mybatis-generator代码生成器 Jqcode:命令行使用Mybatis-generator代码生成器 Jqcode:SpringMVC工作原理 Jqcode:MD5Utils Jqcode:mysql中自连接查询与case-when-then用法 Jqcode:json相关转换 Jqcode:使用Pipeline管道技术快速写入百万级别缓存数据