欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

常用脚本整理

程序员文章站 2023-03-17 23:37:14
九、shell脚本清理磁盘空间 添加定时任务: crontab -e 加:0 0 */4 * * cd /home/script && sh free_space.sh >> /tmp/free_space.log 2>&1 & (每4天清理一次) 十、shell 脚本监控磁盘空间,邮件报警(容量超 ......

 

 

 

九、shell脚本清理磁盘空间

#!bin/bash                                                                                      
#author:jonychen

date=$(date +%y-%m-%d)
echo start ${date} task
aa=`df -h | awk '{print$5}'|sed -n "2, 1p"`
bb=`echo ${aa%\%*}`
cc=70
echo ${bb}

if [ ${bb} -ge ${cc} ]
then
    echo start delete big file
    mubiao=(`ls /opt/log/*.log* /opt/log/resin-log/*.log*`)
    for i in ${mubiao[*]}
    do
        echo "" > ${i}
        rm -rf ${i}
    done
    #find /opt/log/ -size +50m -exec rm {} \;
    #find /opt/log/resin-log/ -size +50m -exec rm {} \;
    echo end delete
else
    echo no file need to delete
fi
echo end ${date} task

添加定时任务:

crontab -e

加:0  0  */4  *  *  cd /home/script && sh free_space.sh >> /tmp/free_space.log 2>&1 & (每4天清理一次)

十、shell 脚本监控磁盘空间,邮件报警(容量超过80%就报警)

#!/bin/bash
#author:jonychen
disk_sda2=df -h |sed -n '2p'|awk '{print $5}' |cut -f 1 -d '%' if ((disk_sda2>80)); then echo "this is error" echo date"192.168.40.1061 this is over 80%" |mail -s "disk over 80%,script: sh /home/script/free_space.sh" 1232332@qq.com else echo "this is ok" fi

添加定时任务:

crontab -e

加:*/30 * * * *  /app/script/monitor_disk_space.sh(每30分钟检查一次)

注:

minute hour day month dayofweek command

分钟 小时 天 月 星期

* 代表任意值 */5 代表每五个单位时间执行一次

minute - 从0到59的整数 

         hour - 从0到23的整数 

         day - 从1到31的整数 (必须是指定月份的有效日期)

         month - 从1到12的整数 (或如jan或feb简写的月份)

         dayofweek - 从0到7的整数,0或7用来描述周日 (或用sun或mon简写来表示)

         command - 需要执行的命令(可用as ls /proc >> /tmp/proc或 执行自定义脚本的命令) 
---------------------