ThinkPHP5实现定时任务

ThinkPHP5实现定时任务php

最近使用ThinkPHP5作了个项目,项目中须要定时任务的功能,感受有必要分享下 html

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

步骤以下: windows

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:");     } }app

 

configure函数是在命令行中用list命令列出全部任务的时候回显示的出的提示,execute函数是说要执行的命令,在这里能够直接调用其余函数,完成例如统计等任务工做,而后用output输出到命令行 ide

3.运行test命令 函数

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

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

D: 

cd D:\phpStudy\WWW\OilTycoon 

php think test 

切换到D盘 

切换到项目根目录处 

运行test命令 

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

 

1、windows:

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

cd D:\WWW\ForeManApi php think spider

2、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

相关文章
相关标签/搜索