网站怎么做搜索栏,建设银行辽宁省分行网站,互联网项目推广方案,怎么在线更新wordpress使用 thinkphp6 框架中提供的命令行形式实现定时任务 一、创建一个自定义命令类文件
php think make:command Hello 会生成一个 app\command\Hello.php 命令行指令类#xff0c;我们修改内容如下#xff1a;
?php
declare (strict_types1);namespace app\command;use … 使用 thinkphp6 框架中提供的命令行形式实现定时任务 一、创建一个自定义命令类文件
php think make:command Hello 会生成一个 app\command\Hello.php 命令行指令类我们修改内容如下
?php
declare (strict_types1);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(){// 指令配置//这里 我们 定义了一个叫 hello 的命令到时候我们运行该hello 命令 就会执行该文件下面的execute()方法$this-setName(hello)-setDescription(Say hello);}/*** execute()方法 就是 运行该命令行类的时候要执行的具体业务逻辑代码*/protected function execute(Input $input, Output $output){// 指令输出$output-writeln(hello world);}
}二、在 config/console.php 文件中进行注册命令
php
// ----------------------------------------------------------------------
// | 控制台配置
// ----------------------------------------------------------------------
return [// 指令定义commands [// hello就是下面要使用 php think 运行的具体命令(即 php think hello),// 叫什么自己随意命名,当我们运行php think hello命令后,// 就会执行app\command\Hello类中的execute()方法hello app\command\Hello, ],
];三、运行 hello 命令
php think hello输出
hello world四、编写 crontab 定时任务文件将 hello 命令 添加到定时任务中
关于crontab,请参考另外一篇文章Linux crontab命令详解
# 1、编辑crontab定时任务文件
crontab -e # 2、每天21点50分执行 php think hello命令,如果定时任务执行 发生错误则 将错误日志文件输
50 21 * * * /usr/local/php7.3/bin/php /home/wwwroot/default/tp6_blog/think hello 2 /tmp/cron_error.log 出到 / tmp/cron_error.log 文件中
注意上面的 php 路径以及 thinkphp6 框架的提供的 think 文件路径 根据自身实际情况 换成自己的别一股脑的全部复制最后发现为啥定时任务没有执行该 hello 命令行。。。
五、总结
1、输入 php think make:command Hello(类库文件名字请自行定义)
2、修改 app\command\Hello 中 execute() 方法 自己的业务逻辑代码
3、在 config/console.php 注册对应的命令
4、将命令 添加到 crontab 定时任务中