#查看磁盘的大小和输出格式:
1
2
3
4
5
|
$ df -h
Filesystem Size Used Avail Use% Mounted on /dev/vda1 20G 5.8G 13G 31% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/vdb 40G 12G 26G 32% /data
|
#截取/data目录使用磁盘大小的百分比
1
2
3
|
$ max=` df -h | awk 'NR==4' '{print $5 }' | cut -d% -f1`
$ echo $max
32 |
#创建清除日志的脚本
1
2
3
4
5
6
7
8
|
cat clean_log.sh
#!/bin/bash max=` df -h | awk 'NR==4' '{print $5 }' | cut -d% -f1`
if [ "$max" -gt 75 ]; then
echo " " > /data/tomcat/logs/catalina .out
find /data/tomcat/logs/ - type f -name "*.log" -mtime +5 | xargs rm -rf
find /data/tomcat/logs/ - type f -name "localhost_*.txt" -mtime +7 | xargs rm -rf
fi |
#创建计划任务,每1小时运行脚本1次
1
2
|
$ crontab -l
0 * * * * /bin/sh /data/clean_log .sh
|
本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1897950