thinkphp6 指令任务 宝塔定时任务

74 篇文章 0 订阅

php think make:command PullAppFile

<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

/**
 * 
 * @author Administrator
 *
 */
class PullAppFile extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('pullappfile')
            ->setDescription('the pullappfile command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 任务开始
        $output->writeln('Begin task');
        
        //处理任务
        $this->handleTask();
            
        // 任务结束
        $output->writeln('End task');
    }
    
    protected function handleTask(){
        Db::table('bq_ceshi')->insert(['name'=>time()]);
    }
}

<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
    // 指令定义
    'commands' => [
        'pullappfile' => 'app\command\PullAppFile',
    ],
];

 执行指令:php think pullappfile

宝塔定时任务:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ThinkPHP 6 支持基于 Linux crontab 的计划任务,可以通过在系统的 crontab 中设置定时任务实现。以下是实现计划任务的步骤: 1. 在 `app/command` 目录下创建一个 `Crontab.php` 命令文件,用于定义需要执行的计划任务。在文件中通过继承 `\think\console\Command` 类和实现 `configure` 和 `execute` 方法来定义计划任务。 ```php <?php namespace app\command; use think\console\Command; use think\console\Input; use think\console\Output; class Crontab extends Command { protected function configure() { $this->setName('crontab')->setDescription('Crontab Command'); } protected function execute(Input $input, Output $output) { // 执行计划任务的代码 $output->writeln('Crontab Command'); } } ``` 2. 在 `app/command.php` 文件中注册定义的计划任务。 ```php <?php // 注册命令 return [ 'app\command\Crontab', ]; ``` 3. 在 Linux 系统的 crontab 中设置定时任务,例如: ``` * * * * * /usr/local/php/bin/php /path/to/think crontab >/dev/null 2>&1 ``` 该命令表示每分钟执行一次 `/path/to/think crontab` 命令,其中 `/path/to/think` 为 ThinkPHP 6 项目的入口文件所在的路径,`crontab` 为需要执行的计划任务的名称,`>/dev/null 2>&1` 表示将输出重定向到空设备,以避免输出干扰 crontab 的正常运行。 注意:在使用 crontab 执行定时任务时,需要注意 PHP 环境变量的设置,以及 PHP 执行权限的问题,否则可能会导致计划任务无法正常执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅坞茶坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值