php tp5+crontab 定时任务 转载自https://blog.csdn.net/qq_16059847/article/details/82351469

 
方式一:sh(最简洁暴力):

    #!/bin/bash
     
    // 1.执行thinkphp5下到index方法
    /usr/bin/php  /home/wwwroot/default/tp5/public/index.php  /index/index/index
     
    // 2.执行thinkphp5下的test方法
    /usr/bin/php  /home/wwwroot/default/tp5/public/index.php  /index/index/test
     
    // 执行普通文件
    /usr/bin/php  /root/learnScript/a.php

方式二:tp5

 
1.新建command文件

在application/模块/新建一个command文件夹/Test.class.php

    <?php
    namespace app\admin\command;
     
    use think\console\Command;
    use think\console\Input;
    use think\console\Output;
     
    class Test extends Command
    {
        protected function configure(){
            $this->setName('Test')->setDescription("计划任务 Test");
        }
     
        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 "test\r\n";
        }
    }

 
2.配置command.php文件,位置在application/command.php

    <?php
    return ['app\admin\command\Test'];

3.运行test命令

打开命令行,运行php think Test命令test命令execute方法中运行的方法就会运行
4.crontab定时执行

先在项目根目录创建一个crond.sh的执行文件,输入下面的内容(加上php程序的目录)

    #!bin/bash
    cd bash的目录
    php think test

开放执行权限:chmond u+x crond.sh

然后运行下面的教程,配置计划任务:

命令行:crontab -e  

然后再界面输入:*/1 * * * * bash 你的目录/crond.sh

保存退出

然后重启crond:

命令:service crond restart

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值