Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用bash脚本发送邮件_Linux_Bash_Email - Fatal编程技术网

Linux 使用bash脚本发送邮件

Linux 使用bash脚本发送邮件,linux,bash,email,Linux,Bash,Email,对通过bash脚本发送的有关磁盘空间的邮件有疑问 每30,59分钟crontab启动bash脚本,如果磁盘空间达到90%,则发送电子邮件。现在2个文件系统达到90%,每次crontab启动,我都会收到关于同一问题的相同电子邮件。如何更新脚本以不向我发送相同的电子邮件,但仅当光盘空间在90-100%的限制范围内变化时 谢谢 df -h | grep -v '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read outpu

对通过bash脚本发送的有关磁盘空间的邮件有疑问

每30,59分钟crontab启动bash脚本,如果磁盘空间达到90%,则发送电子邮件。现在2个文件系统达到90%,每次crontab启动,我都会收到关于同一问题的相同电子邮件。如何更新脚本以不向我发送相同的电子邮件,但仅当光盘空间在90-100%的限制范围内变化时

谢谢

df -h | grep -v '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
echo $output
partition=$(echo $output | awk '{print $2}')
if [ $usep -ge $ALERT ] ; then
echo "Running out of space \"$partition ($usep%)\" on server $(hostname), $(date)" | \
mail -s "Alert: Almost out of disk space $usep%" $ADMIN
fi
done
那么:

report=$(
    df -hP | awk -v alert="$ALERT" -v h="$(hostname)" -v d="$(date)" '
        (!/^Filesystem|tmpfs|cdrom/) && $5 > alert {
            printf "Running out of space \"%s %d\" on server %s, %s\n", $1, $5, h, d
        }
    '
)
if [[ "$report" ]]; then
    mail -s "Alert: Almost out of disk space $usep%" "$ADMIN" <<<"$report"
fi
报告=$(
df-hP | awk-v alert=“$alert”-v h=“$(主机名)”-v d=“$(日期)”
(!/^Filesystem | tmpfs | cdrom/)和&&$5>警报{
printf“在服务器%s,%s\n”上空间不足\%s%d\,$1,$5,h,d
}
'
)
如果[“$report”];然后

mail-s“警报:磁盘空间几乎用完$usep%”“$ADMIN”将以前使用的空间百分比存储在文件中,将其读入数组,最后写入新的百分比

#!/bin/bash

## File with partitions and percent full
last_usep=/path/file

## If the file exists, read lines into array
if [[ -f $last_usep ]] ; then

    ## Associative array
    declare -A map_usep

    while read line ; do

        partition=$(echo $line | awk '{print $1}')
        usep=$(echo $line | awk '{print $2}')
        map_usep[$partition]=$usep   

    done < $last_usep
fi

## Check partitions
df -h | \
grep -v '^Filesystem|tmpfs|cdrom' | \
awk '{ print $5 " " $1 }' | \
while read output; do

    usep=$(echo $output | awk '{print $1}')
    partition=$(echo $output | awk '{print $2}')

    ## If "usep" has not changed, skip to next iteration. Otherwise, store the new percentage
    if [[ $usep== ${map_usep[$partition]} ]] ; then 
        continue
    else
        map_usep[$partition]=$usep
    fi

    if [ $usep -ge $ALERT ] ; then

        echo "Running out of space \"$partition ($usep%)\" on server $(hostname), $(date)" | \
    mail -s "Alert: Almost out of disk space $usep%" $ADMIN

    fi
done

## Update the file with new percentages
\rm $last_usep
touch $last_usep
for partition in "${!usep[@]}" ; do
    echo $partition ${usep[$partition]} >> $last_usep
done
#/bin/bash
##包含分区和已满百分比的文件
last_usep=/path/file
##如果文件存在,则将行读入数组
如果[-f$last_usep]];然后
##关联数组
声明-一张地图
读行时;做
分区=$(echo$行| awk'{print$1}')
usep=$(echo$行| awk'{print$2}')
map_usep[$partition]=$usep
完成<$last\u usep
fi
##检查分区
df-h|\
grep-v'^Filesystem | tmpfs | cdrom'|\
awk'{打印$5”“$1}'|\
同时读取输出;做
usep=$(echo$output | awk'{print$1}')
分区=$(echo$output | awk'{print$2}')
##如果“usep”没有更改,请跳到下一次迭代。否则,存储新的百分比
如果[[$usep==${map_usep[$partition]}]];然后
持续
其他的
map_usep[$partition]=$usep
fi
如果[$usep-ge$ALERT];然后
echo服务器$(主机名),$(日期)上的“$partition($usep%)\”\
邮件-s“警报:磁盘空间几乎用完$usep%”$ADMIN
fi
完成
##用新的百分比更新文件
\rm$last_usep
触摸$last\u usep
对于“${!usep[@]}”中的分区;做
echo$partition${usep[$partition]}>>$last\u usep
完成