Linux文件大小查看及定时删除任务

linux磁盘已满,查看哪个文件占用多?

使用df -h查看磁盘空间占用情况
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 799M 3.1M 796M 1% /run
/dev/vda1 99G 99G 0G 100% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 799M 0 799M 0% /run/user/0

使用du  -sh * 命令也可以列出当前文件以及文件夹的大小。这个命令要注意:sh与*之前要有个空格的。列出home目录所有文件大小的总和命令为:du -s  /home或du -sh /home

使用du  -s /* | sort -nr命令查看那个目录占用空间大
9999500 /root
2711464 /usr
794104 /var
0 /sys
0 /proc
0 /initrd.img.old
0 /initrd.img
0 /dev

然后哪个目录占用多 再通过du -s /root/* | sort -nr 一层层深入排查,找到占用文件多的地方。


使用du  -h   /opt 查看当前目录下文件大小情况

[root@bogon /]# du -h  /opt
64M /opt/script
4.0K /opt/touch/__pycache__
36K /opt/touch/apps/configs/__pycache__
160K /opt/touch/extra_apps/DjangoUeditor/static/ueditor/lang/en/images
80M /opt/touch
224M /opt

 

find /root/srs-master/trunk/objs -mtime +1 -name "srs.log" -exec rm -Rf {} \; //删除一天前文件
find /opt/script -mtime -1 -name "touchrnb.log" -exec rm -Rf {} \; //即刻删除该文件

 

抓包规则:

http and ip.addr == 192.168.1.106 and tcp.port == 8080

 

安装定时包

yum install crontabs

设置定时任务:

crontab -e

20 08 * * *  /opt/clear.sh //每天上午8:20 执行脚本

进入 /opt/目录下

vi clear.sh 添加以下命令

#!/bin/bash
echo “开始清除日志文件-------”
find /opt/script -mtime -1 -name "touchrnb.log" -exec rm -Rf {} \;

 

记得给clear.sh授权

chmod  +x clear.sh

 

基本格式 :
*  *  *  *  *  command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
crontab文件的一些例子:
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重启apache。
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重启apache。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的1 : 10重启apache。
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重启apache。
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小时重启apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11点到早上7点之间,每隔一小时重启apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4号与每周一到周三的11点重启apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一号的4点重启apache

查看定时任务日志

vi  /var/log/cron

Sep 27 02:24:48 localhost crontab[16079]: (root) BEGIN EDIT (root)
Sep 27 02:25:24 localhost crontab[16079]: (root) REPLACE (root)
Sep 27 02:25:24 localhost crontab[16079]: (root) END EDIT (root)
Sep 27 02:25:31 localhost crontab[16082]: (root) BEGIN EDIT (root)
Sep 27 02:25:55 localhost crontab[16082]: (root) REPLACE (root)
Sep 27 02:25:55 localhost crontab[16082]: (root) END EDIT (root)
Sep 27 02:30:01 localhost CROND[16100]: (root) CMD (/opt/clear.sh)

 

posted @ 2018-09-27 10:22  热心市民~菜先生  阅读(1179)  评论(0编辑  收藏  举报