ThinkPHP5实现定时任务

最近使用ThinkPHP5做了个项目,项目中需要定时任务的功能,感觉有必要分享下

TP5做定时任务使用到command.php的

步骤如下:

1.配置command.php文件,位于application/command.php

<?php namespace app\home\command; use think\console\Command; use think\console\Input; use think\console\Output; class Test extends Command {     protected function configure()     {         $this->setName('test')->setDescription('Here is the remark ');     }     protected function execute(Input $input, Output $output)     {  echo '已经开始了';         $output->writeln("TestCommand:");     } }

configure函数是在命令行中用list命令列出所有任务的时候回显示的出的提示,execute函数是说要执行的命令,在这里可以直接调用其他函数,完成例如统计等任务工作,然后用output输出到命令行

3.运行test命令

打开命令行,运行php think test命令test命令execute方法中运行的方法就会运行

4.在入口文件处,新建bat文件,内容如下

D:

cd D:\phpStudy\WWW\OilTycoon

php think test

切换到D盘

切换到项目根目录处

运行test命令

5.把bat文件加入到服务器的定时任务中

一、windows:

// 这里是跳转到项目目录

cd D:\WWW\ForeManApi php think spider

二、linux:

// 为think添加执行权限chmod +x /home/wwwroot/default/www.xxx.com/think crontab -e // 每一小时执行一次 0 */1 * * * /usr/local/php/bin/php /home/wwwroot/default/www.xxx.com/think spider

最新文章

  1. ANGULAR $HTTP请求【转】
  2. react入门(2)
  3. 自动创建WIN32下多级子目录的C++封装类
  4. js小例子(标签页)
  5. mysql中case用法
  6. Xamarin.Android 入门之:Bind java的jar文件+Android显示gif图片
  7. Oracle-查看oracle是否有表被锁
  8. HTTP/2 常见问题回答
  9. 【UVAlive 3989】 Ladies&#39; Choice (稳定婚姻问题)
  10. [转]PHP echo, print, printf, sprintf函数的区别和使用
  11. linq 跨库查询
  12. Linux下的命令行上网
  13. git工具使用方法及常用命令
  14. AtCoder Grand Contest 016
  15. bugku web 变量1
  16. @RequestBody注解的参数仅仅读取一次的问题解决。
  17. Oracle中的一些语句
  18. makefile文件。批处理文件。
  19. tkprof参数详解
  20. nginx代理socket tcp/udp

热门文章

  1. CF EDU - E. Lomsat gelral 树上启发式合并
  2. BZOJ-3343教主的魔法+分块(大块排序二分)
  3. hdu 5495 LCS(并查集)
  4. Play on Words UVA - 10129
  5. 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索
  6. javascript 多个异步处理解决方法
  7. shell中日期循环的方式
  8. FJUT2019暑假周赛一题解
  9. django模型层之多表关系
  10. chrome,firefox浏览器字体发虚解决办法