使用命令行形式实现定时任务
一、创建一个自定义命令类文件
php think make:command Hello hello
会生成一个app\command\Hello命令行指令类,我们修改内容如下:
<?phpdeclare (strict_types = 1);namespace app\command;use think\console\Command;use think\console\Input;use think\console\input\Argument;use think\console\input\Option;use think\console\Output;class Hello extends Command{protected function configure(){// 指令配置$this->setName('hello')->setDescription('Say Hello');}protected function execute(Input $input, Output $output){// 指令输出$output->write('hello world');}}
二、配置config/console.php文件
<?phpreturn ['commands' => ['hello' => 'app\command\Hello',]];
三、运行hello命令
php think hello
输出
hello world
书写脚本文件
每一小时运行
* */1 * * * /etc/php think hello