【问题标题】:Cron job to delete files created before a specific time用于删除在特定时间之前创建的文件的 Cron 作业
【发布时间】:2011-05-18 22:54:25
【问题描述】:

我有这个 cron 作业来清空我每天一次的临时文件夹:

rm -rf /home/username/public_html/temp/*

我希望这个 cron 作业删除在它运行 5 分钟及以上之前创建的所有文件,以确保我的脚本不再需要这些文件。

假设我将 cron 作业设置为每天上午 10:00 运行 .. 我希望它删除该文件夹中在上午 09:55 之前创建的所有文件

非常感谢各位专家!

【问题讨论】:

    标签: php linux cron cpanel


    【解决方案1】:

    如果你使用的是 GNU find,试试这个:

    find /home/username/public_html/temp -type f -mmin +5 -delete
    

    应该也适用于 FreeBSD 或许多其他版本的find

    【讨论】:

      【解决方案2】:

      替代解决方案是:-

      point cron job 每天执行一次 php 文件。

       $dir = 'your/directory/';
        foreach(glob($dir.'*.*') as $v){
        $last_modified = filemtime($v);//get the last modified time of file
        $fmtime=date("h:i:s", $last_modified);
        $currentTime=date("h:i:s");
      
        if (//check here if your file is created before 09:55Am)
        unlink($v);
      
        }
      

      谢谢。

      【讨论】:

      • 全球*.*??我认为你的 DOS 正在显示。这只会匹配名称中带有点的文件。
      【解决方案3】:

      类似于 Jeffreys 的回答,我建议这样:

      for i in `find /home/username/public_html/temp -type f -mmin +5`
          do rm $i
      done
      

      【讨论】:

        猜你喜欢
        • 2011-02-03
        • 1970-01-01
        • 2013-07-10
        • 1970-01-01
        • 1970-01-01
        • 2019-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多