1 : 在 application/crontab 下新建 command 目录,所有要执行的任务文件都放在这下面,然后在command下新建要执行的任务文件 Task.php
2:在 application 目录下找到 command.php , 在添加以下代码
return [
'app\crontab\command\Task',
];
3: 在task.php 下编写要执行的计划任务的代码
<?php
namespace app\crontab\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;
class Task extends Command
{
protected function configure()
{
$this->setName('Task')
->setDescription('task定时任务');
}
protected function execute(Input $input, Output $output)
{
$output->writeln("TaskCommand:start....");
$output->writeln("TaskCommand:start:end....");
}
}
4 : 在linux下设置crontab定时计划任务 (我的项目文件放在/var/www/mytp5)
crontab -l //计划任务列表
crontab -e //编辑新增
crontab -r //删除