ThinkPHP 定时任务
在 application/admin/command 目录下新建一个 Test.php 文件
namespace app\admin\command; use think\console\Command; use think\console\Input; use think\console\Output; use think\Db; class Test extends Command { protected function configure(){ $this->setName('test')->setDescription("计划任务 Test"); } // 调用Test 这个类时,自动调用 execute 这个方法 protected function execute(Input $input, Output $output){ $output->writeln('Date Crontab job start...'); /*** 这里写计划任务列表集 START ***/ $this->test(); /*** 这里写计划任务列表集 END ***/ $output->writeln('Date Crontab job end...'); } private function test() { echo '定时任务成功啦~'; } }
修改 application/command.php
return [ 'app\admin\command\Test' ];
进入项目目录 (并非是根目录) ,使用命令查看
php think
运行定时任务
php think test
Liunx 中编辑crontab
# 编辑crontab crontab -e # 进入编辑模式后添加如下内容 * * * * * php /home/wwwroot/demo.com/think test # 每分钟执行一次计划任务 # 保存退出后重启crontab /etc/init.d/crond restart
效果图如下:
如果没有使用ThinkPHP 自身的定时任务,需要执行指定控制器,在Liunx 中编辑 crontab
* * * * * /usr/bin/php /www/yoursite/public/index.php /tool/Crontab/execCrontab > /dev/null 2>&1 &
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战