tmp目录自动清除和tmpwatch命令

默北 Linux 运维工具 运维案例tmp目录自动清除和tmpwatch命令已关闭评论43,51916字数 1222阅读4分4秒阅读模式

习惯性的将一些临时文件放在tmp目录下,让其自然删除。同时,为了保证tmp目录不爆满,系统默认情况下每日会处理一次tmp目录文件,原理就是使用了tmpwatch。在系统最小安装情况下,这个功能是没有安装的。
安装:

# yum install tmpwatch.x86_64

安装后会在/etc/cron.daily/目录下生成一个tmpwatch文件。内容如下:文章源自运维生存时间-https://www.ttlsa.com/linux/the-tmp-directory-is-automatically-cleared-and-the-tmpwatch-command/

# 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

从代码中可看出,tmp目录会删除240小时未访问过的文件。文章源自运维生存时间-https://www.ttlsa.com/linux/the-tmp-directory-is-automatically-cleared-and-the-tmpwatch-command/

tmpwatch工具从指定的目录中递归地搜索并删除指定的目录中一段时间未访问的文件。文章源自运维生存时间-https://www.ttlsa.com/linux/the-tmp-directory-is-automatically-cleared-and-the-tmpwatch-command/

tmpwatch参数说明:文章源自运维生存时间-https://www.ttlsa.com/linux/the-tmp-directory-is-automatically-cleared-and-the-tmpwatch-command/

-u, --atime		基于访问时间来删除文件,默认的。
-m, --mtime		基于修改时间来删除文件。
-c, --ctime		基于创建时间来删除文件,对于目录,基于mtime。
-M, --dirmtime	删除目录基于目录的修改时间而不是访问时间。
-a, --all		删除所有的文件类型,不只是普通文件,符号链接和目录。
-d, --nodirs	不尝试删除目录,即使是空目录。
-d, --nosymlinks	不尝试删除符号链接。
-f, --force		强制删除。
-q, --quiet		只报告错误信息。
-s, --fuser		如果文件已经是打开状态在删除前,尝试使用“定影”命令。默认不启用。
-t, --test		仅作测试,并不真的删除文件或目录。
-U, --exclude-user=user	不删除属于谁的文件。
-v, --verbose	打印详细信息。
-x, --exclude=path	排除路径,如果路径是一个目录,它包含的所有文件被排除了。如果路径不存在,它必须是一个绝对路径不包含符号链接。
-X, --exclude-pattern=pattern	排除某规则下的路径。

[warning]对于mysql服务器,如果将pid和socket文件创建在tmp目录下,要将这两个文件排除在外,否则mysql重启或使用socket文件登录时,提示找不到文件。可通过-U mysql[/warning]文章源自运维生存时间-https://www.ttlsa.com/linux/the-tmp-directory-is-automatically-cleared-and-the-tmpwatch-command/

转载请注明来之运维生存时间:https://www.ttlsa.com/html/3323.html文章源自运维生存时间-https://www.ttlsa.com/linux/the-tmp-directory-is-automatically-cleared-and-the-tmpwatch-command/ 文章源自运维生存时间-https://www.ttlsa.com/linux/the-tmp-directory-is-automatically-cleared-and-the-tmpwatch-command/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 08/10/2013 10:15:15
  • 转载请务必保留本文链接:https://www.ttlsa.com/linux/the-tmp-directory-is-automatically-cleared-and-the-tmpwatch-command/
  • tmpwatch
  • tmp目录自动清除