a.App/模塊/ 下建立command文件夾php
b.我這邊是建立在crontab模塊裏面,在command文件夾下建立一個Task.php文件(具體名字本身根據需求定)linux
c.複製下面的代碼到Task.phpapp
<?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('定時計劃測試:每分鐘插入一條數據'); } protected function execute(Input $input, Output $output) { // 輸出到日誌文件 $output->writeln("TestCommand:"); // 定時器須要執行的內容 $data = [ 'name'=>'zxm22', 'addr'=>'上海' ]; db('blog')->insert($data); $output->writeln("end...."); } }
crontab -l //計劃任務列表
crontab -e //編輯新增
crontab -r //刪除測試