thinkphp5.1 創建定時任務


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM