PHP创建定时任务 sh+php和tp5+crontab两种方式

方式一: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

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

return ['app\admin\command\Test'];

3.运行test命令

打开命令行,运行php think Test命令test命令execute方法中运行的方法就会运行

你可能感兴趣的:(PHP创建定时任务 sh+php和tp5+crontab两种方式)