Linux 查看空间使用情况的实例详解

 更新时间:2017年10月10日 14:07:44   作者:猫头鹰工作室  
这篇文章主要介绍了Linux 查看空间使用情况的实例详解的相关资料,希望通过本文能帮助到大家,让大家理解掌握如何实现,需要的朋友可以参考下

Linux 查看空间使用情况的实例详解

在日常的Linux巡检中,我们会遇到文件系统目录使用空间很高的情况,例如如下利用”df -h “查看到根目录空间使用超过80%。而我们仅仅知道是根目录空间使用过高,这样是不够的。还需要知道是目录还是文件让根目录空间使用过高。通常我们使用的命令是”du -sh *”。

第一步:查看Linux系统的文件系统使用情况,如下可以看到根目录”/”已经使用81%。


[root@hostname ~]# df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda6       67G  51G  13G 81% /
tmpfs         16G 152K  16G  1% /dev/shm
/dev/sda3       99G 188M  94G  1% /arch
/dev/sda1       197G 188M 187G  1% /u01
/dev/sda2       197G  50G 138G 27% /u02


第二步:进入根目录,利用du -sh 命令查询各个目录或者文件占用空间的情况,因为/u02是独立的文件系统,所以可以忽略。我们关注的是root这个目录,占用了空间47G。

[root@hostname ~]# cd /

[root@hostname /]# du -sh *
20K   arch
7.6M  bin
27M   boot
324K  dev
36M   etc
36K   home
136M  lib
26M   lib64
16K   lost+found
4.0K  media
0    misc
4.0K  mnt
0    net
8.0K  opt
4.0K  orbit-root
--省略/proc目录统计的报错
0    proc
47G   root  ##根目录文件系统使用最大的目录
15M   sbin
0    selinux
4.0K  srv
0    sys
80K   tmp
20K   u01
49G   u02

第三步:进入root目录,其实也是root用户的主目录。我们从ls命令可以猜到可能是【apache-tomcat-6.0.36-linux】这个tomcat的程序目录占用比较多。

[root@hostname /]# cd root
[root@hostname ~]# ls
anaconda-ks.cfg apache-tomcat-6.0.36-linux Desktop Documents Downloads install.log install.log.syslog Music Pictures Public Templates Videos

第四步:通过统计发现,确实是【apache-tomcat-6.0.36-linux】这个目录占用了47G。但是到此问题还没有结束,因为既然是应用程序的目录,根据我的经验很可能是tomcat的日志文件占用空间比较多。

[root@hostname ~]# du -sh *
4.0K  anaconda-ks.cfg
47G   apache-tomcat-6.0.36-linux
4.0K  Desktop
4.0K  Documents
4.0K  Downloads
56K   install.log
12K   install.log.syslog
4.0K  Music
4.0K  Pictures
4.0K  Public
4.0K  Templates
4.0K  Videos

第五步:既然看到有tomcat的目录,很可能这个tomcat运行着,利用ps命令查看是否有tomcat的进程,一般可以过滤java这个关键词。如下,果然有tomcat在运行着。那么很可能占用空间最多就是tomcat的日志文件。

[root@hostname ~]# ps -ef|grep java
root   4879 4836 0 08:26 pts/0  00:00:00 grep java
root   11864   1 47 Aug16 ?    26-03:11:29 /usr/bin/java -Djava.util.logging.config.file=/root/apache-tomcat-6.0.36-linux/conf/logging.properties -Xms512m -Xmx1024m -XX:PermSize=128M -XX:MaxPermSize=256m -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/root/apache-tomcat-6.0.36-linux/endorsed -classpath /root/apache-tomcat-6.0.36-linux/bin/bootstrap.jar -Dcatalina.base=/root/apache-tomcat-6.0.36-linux -Dcatalina.home=/root/apache-tomcat-6.0.36-linux -Djava.io.tmpdir=/root/apache-tomcat-6.0.36-linux/temp org.apache.catalina.startup.Bootstrap start

第六步:进入tomcat的程序目录,查看哪个目录占用的空间最大。如下,发现是一个【fare】的目录占用了46G,而【logs】是日志目录却占用了23M。

[root@hostname ~]# cd apache-tomcat-6.0.36-linux
[root@hostname apache-tomcat-6.0.36-linux]# ls
backup bin conf fare lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@hostname apache-tomcat-6.0.36-linux]# du -sh *
92K   backup
882M  bin
108K  conf
46G   fare
6.8M  lib
40K   LICENSE
23M   logs
4.0K  NOTICE
12K   RELEASE-NOTES
16K   RUNNING.txt
7.9M  temp
188M  webapps
7.1M  work

第七步:继续一探究竟【fare】目录里面到底存放的是什么类型的目录或者文件。如下看到有很多类似日志文件,如common.log和pricing.log。通过和开发人员沟通之后,确定确实日志文件。

[root@hostname apache-tomcat-6.0.36-linux]# cd fare
[root@hostname fare]# ls
common.log       common.log.2017-09-21 common.log.2017-10-03   pricing.log.2017-09-10 pricing.log.2017-09-22 pricing.log.2017-10-04
common.log.2017-09-10 common.log.2017-09-22 common.log.2017-10-04   pricing.log.2017-09-11 pricing.log.2017-09-23 pricing.log.2017-10-05
common.log.2017-09-11 common.log.2017-09-23 common.log.2017-10-05   pricing.log.2017-09-12 pricing.log.2017-09-24 pricing.log.2017-10-06
common.log.2017-09-12 common.log.2017-09-24 common.log.2017-10-06   pricing.log.2017-09-13 pricing.log.2017-09-25 pricing.log.2017-10-07
common.log.2017-09-13 common.log.2017-09-25 common.log.2017-10-07   pricing.log.2017-09-14 pricing.log.2017-09-26 pricing.log.2017-10-08
common.log.2017-09-14 common.log.2017-09-26 common.log.2017-10-08   pricing.log.2017-09-15 pricing.log.2017-09-27 pricing.log.2017-10-09
common.log.2017-09-15 common.log.2017-09-27 common.log.2017-10-09   pricing.log.2017-09-16 pricing.log.2017-09-28 spring.log
common.log.2017-09-16 common.log.2017-09-28 common.zip        pricing.log.2017-09-17 pricing.log.2017-09-29 struts2.log
common.log.2017-09-17 common.log.2017-09-29 framework.log       pricing.log.2017-09-18 pricing.log.2017-09-30 xwork2.log
common.log.2017-09-18 common.log.2017-09-30 hibernate.log       pricing.log.2017-09-19 pricing.log.2017-10-01
common.log.2017-09-19 common.log.2017-10-01 hibernate.log.2017-01-03 pricing.log.2017-09-20 pricing.log.2017-10-02
common.log.2017-09-20 common.log.2017-10-02 pricing.log        pricing.log.2017-09-21 pricing.log.2017-10-03

第八步:继续查看哪个目录或者文件占用的空间最大,发现都是common.log和pricing.log相关的文件占用的空间最大,都是700M以上。至此,已经查明占用根目录”/”空间最多的原因是tomcat的日志文件太多,而且每一个日志文件都很大。

[root@hostname fare]# du -sh *|sort -h
0    framework.log
0    hibernate.log.2017-01-03
0    spring.log
0    struts2.log
0    xwork2.log
4.0K  hibernate.log
17M   common.zip
215M  pricing.log
216M  common.log
667M  pricing.log.2017-10-08
668M  common.log.2017-10-08
674M  common.log.2017-10-05
674M  pricing.log.2017-10-05
678M  common.log.2017-10-07
678M  pricing.log.2017-10-07
679M  common.log.2017-10-06
679M  pricing.log.2017-10-06
683M  common.log.2017-10-04
683M  pricing.log.2017-10-04
690M  pricing.log.2017-09-10
691M  common.log.2017-09-10
711M  common.log.2017-09-12
711M  pricing.log.2017-09-12
719M  pricing.log.2017-09-11
720M  common.log.2017-09-11
737M  common.log.2017-09-30
737M  pricing.log.2017-09-28
737M  pricing.log.2017-09-30
738M  common.log.2017-09-28
742M  common.log.2017-09-29
742M  pricing.log.2017-09-29
744M  pricing.log.2017-10-09
745M  common.log.2017-10-09
751M  common.log.2017-10-01
751M  pricing.log.2017-10-01
754M  common.log.2017-09-13
754M  pricing.log.2017-09-13
760M  common.log.2017-10-03
760M  pricing.log.2017-10-03
783M  pricing.log.2017-10-02
784M  common.log.2017-10-02
791M  pricing.log.2017-09-18
792M  common.log.2017-09-18
797M  common.log.2017-09-27
797M  pricing.log.2017-09-27
804M  common.log.2017-09-17
804M  common.log.2017-09-19
804M  pricing.log.2017-09-17
804M  pricing.log.2017-09-19
808M  common.log.2017-09-26
808M  pricing.log.2017-09-26
815M  pricing.log.2017-09-24
816M  common.log.2017-09-24
821M  common.log.2017-09-14
821M  pricing.log.2017-09-14
821M  pricing.log.2017-09-23
822M  common.log.2017-09-23
826M  common.log.2017-09-25
826M  pricing.log.2017-09-25
827M  common.log.2017-09-16
827M  pricing.log.2017-09-15
827M  pricing.log.2017-09-16
828M  common.log.2017-09-15
831M  common.log.2017-09-22
831M  pricing.log.2017-09-22
851M  common.log.2017-09-21
851M  pricing.log.2017-09-21
860M  common.log.2017-09-20
860M  pricing.log.2017-09-20

 第九步:从文件的命令规律,可以看出日志文件日期保留一个月,检查root用户是否有定时删除日志文件计划。如下命令查看所示,确实有每天凌晨03:15进行日志文件删除的计划。但是由于日志文件太多和根目录空间大小原因,导致根目录空间使用比例过高。在有部署自动监控工具,如zabbix等,会自动发送告警。


[root@hostname ~]# crontab -l
15 3 * * * /u02/shell/rm_log_pricing.sh
[root@hostname ~]# cat /u02/shell/rm_log_pricing.sh
find /root/apache-tomcat-6.0.36-linux/bin/fare/ -name "common.log.2*" -mtime +30 | xargs rm -f
find /root/apache-tomcat-6.0.36-linux/bin/fare/ -name "pricing.log.2*" -mtime +30 | xargs rm -f

总结:

从运维的角度,不建议tomcat等应用程序部署在和Linux操作系统相关的文件系统中,尤其是直接部署在根目录下。一般都建议使用独立的文件系统来部署应用程序,无论从IO性能监控,文件系统空间使用监控都是有利的。

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • Linux使用crontab定时执行脚本任务的流程详解

    Linux使用crontab定时执行脚本任务的流程详解

    在Linux中可以使用crontab来定时执行脚本,crontab是一个用于管理定时任务的工具,可以让在特定的时间间隔内自动运行脚本或命令,本文我们将使用cron来定期执行一个写入命令或脚本,需要的朋友可以参考下
    2023-10-10
  • linux下安装nginx(图文教程)

    linux下安装nginx(图文教程)

    本篇文章主要介绍了linux下安装nginx,Nginx是一款轻量级的Web服务器。有需要的朋友可以了解一下。
    2016-11-11
  • 安装redhat 8.0红帽系统的图文教程(小白必备)

    安装redhat 8.0红帽系统的图文教程(小白必备)

    这篇文章主要介绍了安装redhat 8.0红帽系统的图文教程(小白必备),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • ubuntu下没有中文输入法的解决办法

    ubuntu下没有中文输入法的解决办法

    今天小编就为大家分享一篇关于ubuntu下没有中文输入法的解决办法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • CentOS安装Python2.7与Python2.6并存的方法

    CentOS安装Python2.7与Python2.6并存的方法

    这篇文章主要介绍了CentOS安装Python2.7与Python2.6并存的方法,非常具有实用价值,需要的朋友可以参考下
    2017-08-08
  • 在Linux上安装和使用Docker的方法

    在Linux上安装和使用Docker的方法

    这篇文章主要介绍了在Linux上安装和使用Docker的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • ubuntu开机后ROS程序自启动问题

    ubuntu开机后ROS程序自启动问题

    这篇文章主要介绍了ubuntu开机后ROS程序自启动问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 浅析CentOS8虚拟机访问Windows10主机文件夹方法

    浅析CentOS8虚拟机访问Windows10主机文件夹方法

    这篇文章主要介绍了CentOS8虚拟机访问Windows10主机文件夹方法,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-10-10
  • 深入浅析Linux轻量级自动运维工具-Ansible

    深入浅析Linux轻量级自动运维工具-Ansible

    这篇文章主要介绍了Linux轻量级自动运维工具-Ansible的相关知识,需要的朋友可以参考下
    2017-09-09
  • Ubuntu系统网络故障排查的方法

    Ubuntu系统网络故障排查的方法

    最近在使用Ubuntu系统的时候碰到一个问题,连接无线网络的时候,发现右上角网络设置中没有 Enable Wi-Fi 这个选项了,所以通过一步步排查,终于找了解决办法,现在分享给大家,有需要的朋友们可以参考借鉴。
    2016-10-10

最新评论