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文件里
    
     
    
    根据上面的步骤就可以把定时任务跑起来了,很简单吧!!!
  • 相关阅读:
    作业七随笔。。
    Jquery 图片走马灯效果原理
    参与招聘面试工作之简历与仪容篇
    无聊系列 C#中一些常用类型与java的类型对应关系
    关于ASP.NET MVC 中JsonResult返回的日期值问题
    最近参与招聘面试的工作总结
    Unix时间戳转日期时间格式,C#、Java、Python各语言实现!
    MVC 拦截器
    Python参考书籍(转载)
    PEP 8风格指南(转载)
  • 原文地址:https://www.cnblogs.com/ivy-zheng/p/11782504.html
Copyright © 2011-2022 走看看