zoukankan      html  css  js  c++  java
  • 定时删除文件夹"$1"下最后修改时间大于当前时间"$2"天的文件

    shell 脚本:

    #!/bin/bash

    now=`date "+%Y-%m-%d_%H:%M:%S"`      #获取当前时间 echo "当前时间: "$now

    now=`date +%s`            #获取当前时间戳   单位:秒 echo "当前时间戳: "$now

    function getdir(){          #遍历文件夹和文件夹下所有的文件

    #    echo "删除文件夹"$1"下最后修改时间大于当前时间"$2"天的文件"    

    for element in `ls $1`    

         do         dir_or_file=$1"/"$element        

               if [ -d $dir_or_file ]        #如果是目录,遍历该目录下的所有文件        

               then            

               getdir $dir_or_file $2        

               elif [ -f $dir_or_file ]       #如果是文件,判断最后修改时间大于当前时间指定天数就删除        

               then            

               zhxgsj=`stat -c %Y $dir_or_file`    #获取文件最后修改时间           

                         if [ `expr $now - $zhxgsj` -gt `expr 24 * 60 * 60 * $2` ]           

                         then           

                         var=`/bin/rm -rf $dir_or_file`           

                         echo $var           

                         echo "删除文件: "$dir_or_file" 最后修改时间: "`date -d @$zhxgsj  "+%Y-%m-%d"`           

                         fi       

                 fi  

        done

    }

    dir="/home/log"

    getdir $dir 30   

    dir="/mydata/tomcat-7.0.79/logs"

    getdir $dir 2

    添加到定时任务:

    # crontab -e

    添加  00 03 * * * bash /home/clear.sh

    # /sbin/service crond restart //重启服务

    定时重启服务器:

    59 07 * * * /sbin/shutdown -r now >> /home/crontab.log
    59 19 * * * /sbin/shutdown -r now >> /home/crontab.log

    安装crontab:

    yum install crontabs

    服务操作说明:

    /sbin/service crond start //启动服务

    /sbin/service crond stop //关闭服务

    /sbin/service crond restart //重启服务

    /sbin/service crond reload //重新载入配置

    查看crontab服务状态:

    service crond status

    手动启动crontab服务:

    service crond start

    查看crontab服务是否已设置为开机启动,执行命令:

    ntsysv

  • 相关阅读:
    EXT FileUploadField 文件上传失败
    CRM自定义页面
    The report server was unable to validate the integrity of encrypted data in the database. (rsCannotValidateEncryptedData) .
    sql存储过程与临时表
    MERGE INTO
    [LeetCode]Merge Two Sorted Lists
    [LeetCode]Search for a Range
    [LeetCode]Length of Last Word
    [LeetCode]Implement strStr()
    [LeetCode]Pascal's Triangle
  • 原文地址:https://www.cnblogs.com/gaobo543013306/p/9651806.html
Copyright © 2011-2022 走看看