TP5的Command 与 Linux的Crontab 简单实现定时任务
1.在Tp5项目中,执行 命令生成command文件和test方法 (服务器中记得设置该文件权限www 750)
php think make:command Test
(或者手动创建)
use think\console\Command; use think\console\Input; use think\console\Output; class Test extends Command { protected function configure() { // 指令配置 $this->setName('Test'); } protected function execute(Input $input, Output $output) { $output->writeln("文件执行成功!!!!") } }
注意这三处名字相同!!! (注意服务器文件也要同步)
2.在command.php中配置test类,然后在控制台执行 即可运行test下的execute()方法
php think Test
3.编辑root文件 定义定时任务
/var/spool/cron
权限600 请勿修改!
4.每分钟执行一次 test文件
语法: 定时时间 + 命令 (*/1 * * * * php thnik Test)
因为不在项目下,所以要用绝对路径
*/1 * * * * /usr/bin/php /www/wwwroot/queue/think Test
5.查看系统email 可以看到执行的异常