zoukankan      html  css  js  c++  java
  • tp5定时任务

    第一步:

    a.App/模块/ 下创建command文件夹

    b.我这边是创建在crontab模块里面,在command文件夹下创建一个Task.php文件(具体名字自己根据需求定)

    c.复制下面的代码到Task.php

    <?php
    namespace appcrontabcommand;
    
    use thinkconsoleCommand;
    use thinkconsoleInput;
    use thinkconsoleinputArgument;
    use thinkconsoleinputOption;
    use thinkconsoleOutput;
    use thinkDb;
    class Task extends Command
    {
        protected function configure()
        {
            $this->setName('task')
                ->setDescription('定时计划测试:每分钟插入一条数据');
        }
    
        protected function execute(Input $input, Output $output)
        {
            // 输出到日志文件
            $output->writeln("TestCommand:");
           // 定时器需要执行的内容
            $data = [
                'name'=>'zxm22',
                'addr'=>'上海'
            ];
            db('blog')->insert($data);
            $output->writeln("end....");
        }
    
    
    }

    第二步:在APP/command.php里面加上

    第三步:在linux下设置crontab定时计划任务(我的项目文件放在/var/www/mytp5)即可

    crontab -l //计划任务列表
    crontab -e //编辑新增
    crontab -r //删除

     可参照:(查看php执行路径  whereis php)

    1、配置command.php文件,目录在application/command.php
    
    2、建立命令类文件,新建application/index/command/Test.php
    
    <?php
    namespace appindexcommand;
     
    use thinkconsoleCommand;
    use thinkconsoleInput;
    use thinkconsoleOutput;
     
    class Test extends Command
    {
        protected function configure()
        {
            $this->setName('test')->setDescription('Here is the remark ');
        }
     
        protected function execute(Input $input, Output $output)
        {
            $output->writeln("TestCommand:");
        }
    }
    3、命令行下运行php think test ,将输出 TestCommand:,说明命令行已经成功
    
    4、在linux系统配置crontab定时任务,一般目录在下图
    
    4、根据自己的用户角色,编辑文件,如我是root用户,打开root文件,配置contab
    
    */1 * * * * cd /项目路径 && /php路径/php think test >> tmp/test.log 2>&1
    
    这句话的意思是没一分钟执行一遍 test/index,日志存储在项目目录的 tmp/test.log文件里
    
     
    
    根据上面的步骤就可以把定时任务跑起来了,很简单吧!!!
  • 相关阅读:
    枚举--分巧克力--蓝桥杯--二分法
    枚举笔记之哈希表 四个平方和
    枚举 蓝桥杯 四个平方数和2--暴力解法 超时
    枚举例题之平方十位数思路无代码
    LeetCode----盛最多水的容器「贪心」
    2020校招笔试
    2020校招美团点评笔试
    2020校招搜狗笔试
    2020网易校招笔试
    2020校招途家名宿开发笔试
  • 原文地址:https://www.cnblogs.com/ivy-zheng/p/11782504.html
Copyright © 2011-2022 走看看