1.在application目錄下創建crontab模塊(統一存放定時任務)
2.在command目錄下創建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('定時計划:每天生成一個日期文件'); } protected function execute(Input $input, Output $output) { file_put_contents(time().'.txt', '當前日期為:'.date('Y-m-d')); } }
3.在application的command.php 文件中添加
return [ 'app\crontab\command\Task', ];
4.我這里用的linux 需要設置 crontab 在linux下 crontab -e 添加定時執行內容
0 0 * * * /usr/bin/php /home/www/
www.test.com/think task
我這里設置每日凌晨的時候創建 用於統計
php 的安裝目錄查看命令 whereis php