项目的运行过程中,有时候需要写一个定时任务,执行一些操作,比如定时更新缓存,备份数据等等等等。今天主要介绍一下thinkphp6.x命令编写shell脚本
在宝塔面板创建一个定时任务,写入一段简单的定时shell脚本(注意修改成自己tp6网站根目录)
1 2 3 | Path=/www/wwwroot/xxx.com cd $Path php think clear |
编写自定义指令
第一步,创建一个自定义命令类文件,运行指令
1 | php think make:command Hello hello |
会生成一个appcommandHello命令行指令类
第二步,修改appcommandHello 中execute函数自己的逻辑代码
例如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php namespace appcommand; use thinkconsoleCommand; use thinkconsoleInput; use thinkconsoleinputArgument; use thinkconsoleinputOption; use thinkconsoleOutput; class Hello extends Command { protected function configure() { $this->setName('hello') ->addArgument('name', Argument::OPTIONAL, "your name") ->addOption('city', null, Option::VALUE_REQUIRED, 'city name') ->setDescription('Say Hello'); } protected function execute(Input $input, Output $output) { $name = trim($input->getArgument('name')); $name = $name ?: 'thinkphp'; if ($input->hasOption('city')) { $city = PHP_EOL . 'From ' . $input->getOption('city'); } else { $city = ''; } $output->writeln("Hello," . $name . '!' . $city); } } |
第三步,配置config/console.php文件
1 2 3 4 5 6 7 | <?php return [ 'commands' => [ 'hello' => 'appcommandHello', ] ]; |
第四步,运行hello命令
1 | php think hello |
具体请参考看云文档-自定义指令
thinkphp6.x常用的命令行
指令 | 描述 |
---|---|
build | 自动生成应用目录和文件 |
help | 帮助 |
list | 指令列表 |
clear | 清除缓存指令 |
run | 启动PHP内置服务器 |
versiON | 查看当前框架版本号 |
具体使用方法和更多命令行,可参考看云文档-命令行
以上这篇宝塔面板添加thinkphp6.x命令执行脚本就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持主机宝贝。