写入大量日志或有大文件占满磁盘,会导致应用程序崩溃。针对这个问题,可以使用一个简单的shell实现磁盘使用率接近阈值时邮件提醒的功能。环境Ubuntu16.04邮件服务安装heirloom-mailxsudoapt-getinstallheirloom-mailx配置smtpvi/etc/s-nail.rc(或nail.rc)根据所使用的对应邮箱服务进行配置,密码一般使用授权码//s-nail.rcsetfrom="XXX@163.com"setsmtp="smtp.163.com"setsmtp-auth-user="XXX@163.com"setsmtp-auth-password="xxx"setsmtp-auth=登录测试发送成功echodate"邮件内容"|heirloom-mailx-vs"title"xxx@163.comshell脚本创建脚本disk-space-notice.sh,如果/dev/sda1磁盘分区使用率超过85%,则发送邮件提醒。//磁盘空间通知.shdev_sda1=`df-h|sed-n'4p'|awk'{print$5}'|cut-f1-d'%'`if((dev_sda1>85));然后回显“VMserver-dev_sha1磁盘使用率超过阈值”|heirloom-mailx-vs">serveralarmnotification"xxx@163.comfi代码简单解释主要用到这个命令df-h|sed-n'4p'|awk'{print$5}'|cut-f1-d'%'df-h查看磁盘占用文件系统capacityusedavailableused%mountpointudev1.9G01.9G0%/devtmpfs393M41M352M11%/run/dev/sda129G23G4.6G84%/tmpfs2.0G10M2.0G1%/dev/shmtmpfs5.0M4.0K5.0M1%/run/locktmpfs2.0G02.0G0%/sys/fs/cgroupfinal,我们要得到的值为84/dev/sda1的使用速率。sed-n'4p'输出/dev/sda1所在的行awk'{print$5}'得到84%的使用率cut-f1-d'%'切断会检测磁盘使用率的shellof%计划任务,添加定时任务,在指定时间内执行一次//crontab-e*/10****/bin/bash/home/ghost/disk-space-notice.sh
