[20201112]tmpwatch 删除文件.txt

  • A+
所属分类:linux技术
摘要

[20201112]tmpwatch 删除文件.txt

–//tmpwatch命令用来removes files which haven’t been accessed for a period of time。
–//一般发布版本用来通过crond调用tmpwatch删除一定时间内不使用的文件。

# cat /etc/cron.daily/tmpwatch
flags=-umc
/usr/sbin/tmpwatch “$flags” -x /tmp/.X11-unix -x /tmp/.XIM-unix
        -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix
        -X ‘/tmp/hsperfdata_*’ 240 /tmp
/usr/sbin/tmpwatch “$flags” 720 /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
    if [ -d “$d” ]; then
        /usr/sbin/tmpwatch “$flags” -f 720 “$d”
    fi
done

–//flags=-umc表示
-u, –atime
    Make the decision about deleting a file based on the file’s atime (access time). This is the default.
    Note that the periodic updatedb file system scans keep the atime of directories recent.

-m, –mtime
    Make the decision about deleting a file based on the file’s mtime (modification time) instead of the atime.

-c, –ctime
    Make the decision about deleting a file based on the file’s ctime (inode change time) instead of the atime; for
    directories, make the decision based on the mtime.

# man

 tmpwatch [-u|-m|-c] [-MUXadfqstvx] [–verbose] [–force] [–all]
                [–nodirs] [–nosymlinks] [–test] [–fuser] [–quiet]
                [–atime|–mtime|–ctime] [–dirmtime] [–exclude <path>]
                [–exclude-user <user>] [–exclude-pattern <pattern>]
                <hours> <dirs>

–//可以使用tmpwatch <hours> <dirs> 快速删除文件。仅仅需要注意第2个参数是小时,必须换算看看。
–//缺省默认是atime.
By default, tmpwatch dates files by their atime (access time), not their mtime (modification time). If files aren’t
being removed when ls -l implies they should be, use ls -u to examine their atime to see if that explains the problem.

–//建议加入-m参数。

# tmpwatch -m [time in hours] [directory]

–//简单测试如下:
# mkdir -p /tmp/test
# touch  -am -d 20100909 aaa
# touch  -am -d 20201113 bbb

# echo  -e “aaanbbb” | xargs -IQ stat Q
  File: `aaa’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 6802h/26626d    Inode: 11534342    Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-09-09 00:00:00.000000000 +0800
Modify: 2010-09-09 00:00:00.000000000 +0800
Change: 2020-11-13 09:08:30.000000000 +0800
  File: `bbb’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 6802h/26626d    Inode: 11534343    Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-13 00:00:00.000000000 +0800
Modify: 2020-11-13 00:00:00.000000000 +0800
Change: 2020-11-13 09:08:47.000000000 +0800

# tmpwatch -m $((24*2)) /tmp/test
–//表示删除2天前修改的文件。

# ls -l /tmp/test
total 0
-rw-r–r– 1 root root 0 2020-11-13 00:00:00 bbb

–//你还可以通过-t参数先浏览看看,测试:
# touch  -am -d 20100909 aaa
# tmpwatch -t -m $((24*2)) /tmp/test
removing file /tmp/test/aaa

# tmpwatch -tv -m $((24*2)) /tmp/test
grace period is 172800
–//24*2*3600 = 172800 ,这个时间竟然是秒数。
cleaning up directory /tmp/test
removing file /tmp/test/aaa

–//这样可以提到替换使用find命令,简单实用。
–//另外补充一点缺省oracle并不删除链接,以及pipe,socket类型文件。如果删除许多oracle rac可能都出现问题。

#  ls -l /tmp/.oracle/ | head
total 0
srwxrwx— 1 grid   oinstall 0 2014-12-11 02:30:20 master_diskmon
srwxr-xr-x 1 grid   oinstall 0 2014-12-11 02:30:18 mdnsd
prw-r–r– 1 root   root     0 2014-10-10 20:53:50 npohasd
srwxrwxrwx 1 grid   oinstall 0 2014-12-11 02:30:26 ora_gipc_gipcd_dm01dbadm01
srwxrwxrwx 1 grid   oinstall 0 2014-12-11 02:30:19 ora_gipc_GPNPD_dm01dbadm01
srwxrwxrwx 1 root   root     0 2014-12-11 02:30:26 ora_gipc_sdm01dbadm01gridgxhospitalCRFM_CLIIPC
srwxrwxrwx 1 root   root     0 2014-12-11 02:30:26 ora_gipc_sdm01dbadm01gridgxhospitalCRFM_SIPC
srwxrwxrwx 1 oracle oinstall 0 2020-06-29 14:37:23 s#101813.1
srwxrwxrwx 1 oracle oinstall 0 2020-06-29 14:37:23 s#101813.2

#  tmpwatch -tv -m 24 /tmp/.oracle/
grace period is 86400
cleaning up directory /tmp/.oracle

[20201112]tmpwatch 删除文件.txt

--//tmpwatch命令用来removes files which haven't been accessed for a period of time。
--//一般发布版本用来通过crond调用tmpwatch删除一定时间内不使用的文件。

# cat /etc/cron.daily/tmpwatch
flags=-umc
/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix
        -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix
        -X '/tmp/hsperfdata_*' 240 /tmp
/usr/sbin/tmpwatch "$flags" 720 /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
    if [ -d "$d" ]; then
        /usr/sbin/tmpwatch "$flags" -f 720 "$d"
    fi
done

--//flags=-umc表示
-u, --atime
    Make the decision about deleting a file based on the file's atime (access time). This is the default.
    Note that the periodic updatedb file system scans keep the atime of directories recent.

-m, --mtime
    Make the decision about deleting a file based on the file's mtime (modification time) instead of the atime.

-c, --ctime
    Make the decision about deleting a file based on the file's ctime (inode change time) instead of the atime; for
    directories, make the decision based on the mtime.

# man

 tmpwatch [-u|-m|-c] [-MUXadfqstvx] [--verbose] [--force] [--all]
                [--nodirs] [--nosymlinks] [--test] [--fuser] [--quiet]
                [--atime|--mtime|--ctime] [--dirmtime] [--exclude <path>]
                [--exclude-user <user>] [--exclude-pattern <pattern>]
                <hours> <dirs>

--//可以使用tmpwatch <hours> <dirs> 快速删除文件。仅仅需要注意第2个参数是小时,必须换算看看。
--//缺省默认是atime.
By default, tmpwatch dates files by their atime (access time), not their mtime (modification time). If files aren't
being removed when ls -l implies they should be, use ls -u to examine their atime to see if that explains the problem.

--//建议加入-m参数。

# tmpwatch -m [time in hours] [directory]

--//简单测试如下:
# mkdir -p /tmp/test
# touch  -am -d 20100909 aaa
# touch  -am -d 20201113 bbb

# echo  -e "aaanbbb" | xargs -IQ stat Q
  File: `aaa'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 6802h/26626d    Inode: 11534342    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-09-09 00:00:00.000000000 +0800
Modify: 2010-09-09 00:00:00.000000000 +0800
Change: 2020-11-13 09:08:30.000000000 +0800
  File: `bbb'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 6802h/26626d    Inode: 11534343    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-13 00:00:00.000000000 +0800
Modify: 2020-11-13 00:00:00.000000000 +0800
Change: 2020-11-13 09:08:47.000000000 +0800

# tmpwatch -m $((24*2)) /tmp/test
--//表示删除2天前修改的文件。

# ls -l /tmp/test
total 0
-rw-r--r-- 1 root root 0 2020-11-13 00:00:00 bbb

--//你还可以通过-t参数先浏览看看,测试:
# touch  -am -d 20100909 aaa
# tmpwatch -t -m $((24*2)) /tmp/test
removing file /tmp/test/aaa

# tmpwatch -tv -m $((24*2)) /tmp/test
grace period is 172800
--//24*2*3600 = 172800 ,这个时间竟然是秒数。
cleaning up directory /tmp/test
removing file /tmp/test/aaa

--//这样可以提到替换使用find命令,简单实用。
--//另外补充一点缺省oracle并不删除链接,以及pipe,socket类型文件。如果删除许多oracle rac可能都出现问题。

#  ls -l /tmp/.oracle/ | head
total 0
srwxrwx--- 1 grid   oinstall 0 2014-12-11 02:30:20 master_diskmon
srwxr-xr-x 1 grid   oinstall 0 2014-12-11 02:30:18 mdnsd
prw-r--r-- 1 root   root     0 2014-10-10 20:53:50 npohasd
srwxrwxrwx 1 grid   oinstall 0 2014-12-11 02:30:26 ora_gipc_gipcd_dm01dbadm01
srwxrwxrwx 1 grid   oinstall 0 2014-12-11 02:30:19 ora_gipc_GPNPD_dm01dbadm01
srwxrwxrwx 1 root   root     0 2014-12-11 02:30:26 ora_gipc_sdm01dbadm01gridgxhospitalCRFM_CLIIPC
srwxrwxrwx 1 root   root     0 2014-12-11 02:30:26 ora_gipc_sdm01dbadm01gridgxhospitalCRFM_SIPC
srwxrwxrwx 1 oracle oinstall 0 2020-06-29 14:37:23 s#101813.1
srwxrwxrwx 1 oracle oinstall 0 2020-06-29 14:37:23 s#101813.2

#  tmpwatch -tv -m 24 /tmp/.oracle/
grace period is 86400
cleaning up directory /tmp/.oracle