如何在bash中发送有关磁盘空间的邮件警报

时间:2018-04-04 12:28:29

标签: linux bash

我可以在Linux中发送简单测试的邮件

echo "body" | mail -s "test" xxx@yahoo.com

但是代码下方不起作用

#!/bin/bash
VAR1=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
VAR2=$(df  /dev/sda1 | grep /dev/sda1 | awk '{ print $5}' | sed 's/%//g')
VAR3=$(df /dev/mapper/centos-var | grep /dev/mapper/centos-var | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=50
TODAY=$(date)
if [ "$VAR1" -gt "$THRESHOLD" ] ; then
  mail -s 'Disk Space Alert'  rupendra@3ess.in  << EOF
  Date of $TODAY
  Your system  partition remaining free space is critically low.
  / partition used is  $VAR1%
  / usr partition used is  $VAR2%
  / var partition used is $VAR3%

1 个答案:

答案 0 :(得分:-1)

您收到了什么错误?

以下一个对我有用,

#!/bin/bash

set -x

VAR1=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
VAR2=$(df /dev/sda1 | grep /dev/sda1 | awk '{ print $5}' | sed 's/%//g')
VAR3=$(df /dev/mapper/centos-var | grep /dev/mapper/centos-var | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=50
TODAY=$(date)

echo "VAR1=$VAR1, THRESHOLD=$THRESHOLD"

if [ "$VAR1" -gt "$THRESHOLD" ] ; then
mail -s 'Disk Space Alert' sachin@zedo.com << EOF
Date of $TODAY
Your system partition remaining free space is critically low.
/ partition used is $VAR1%
/ usr partition used is $VAR2%
/ var partition used is $VAR3%

EOF

fi