linux tmp自动清理

1. 背景

线上机器总是报内存不足,df -h 发现存储空间占满df -i ,发现是tmp中临时文件占满,可以修改系统定时清除tmp文件的时间间隔

tips1 : 指定目录下,列出文件大小

  • du -h --max-depth=1

tips2:  will delete all files and folders older than 100 days 

  • find /tmp/pu* -ctime +100 -exec rm -rf {} +

2. Linux系统自带定时清除

2.1 tmpwatch--删除指令

[root@cp01vm tmp]# whereis tmpwatch
tmpwatch: /usr/bin/tmpwatch /usr/sbin/tmpwatch /usr/share/man/man8/tmpwatch.8.gz
[root@cp01vm tmp]# file /usr/sbin/tmpwatch
/usr/sbin/tmpwatch: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
[root@cp01vm tmp]# more /etc/cron.daily/tmpwatch
#! /bin/sh
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_*' 3d /tmp
/usr/sbin/tmpwatch "$flags" 3d /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
    if [ -d "$d" ]; then
        /usr/sbin/tmpwatch "$flags" -f 3d "$d"
    fi
done

参数含义:

-u 按照文件的最后access时间,即最后访问时间为参考。默认选项。可通过ls -lu查看

-m 按照文件的最后modified时间,即最后修改时间为参考。可通过ls -l查看

-c 按照文件的-ctime时间做参考,ctime更新的条件为写入、更改属主、权限。可通过ls -lc查看

-x /PATH 排除特定目录,即不删除该子目录里的文件

2.2 crontab--定时指令

[root@cp01 tmp] cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

设置定时指令,其中:run-parts 表示执行后续文件夹下所有文件;若不添加该参数,则需要指定具体执行指令文件

比如:30 11 * * * root /etc/cron.daily/tmpwatch

2.3 重启crond服务

service crond start           //启动服务
service crond stop           //关闭服务
service crond restart       //重启服务
service crond reload       //重新载入配置

参考文献:

https://www.cnblogs.com/kerrycode/p/5759941.html

https://blog.csdn.net/zhidetian/article/details/51906335

https://blog.csdn.net/doc_sgl/article/details/41653641

http://blog.sina.com.cn/s/blog_477759e701012fi4.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值