3

This question already has an answer here:

I am writing a simple bash script to check for disk usage on my system. The while loop should only print partitions that are at 85% or above. There are two partitions over 90% but only the first partition is being printed to output.

Code:

#!/bin/bash
admin="foo@bar.com"
threshold="85"

df -h | tail -n +2 | awk '{print $1 " " $5}' | sed 's/.$//' | while read i; do
  part=$(echo $i | awk '{print $1}')
  util=$(echo $i | awk '{print $2}')
    if [ $util -ge $threshold ]; then
      echo $part is ${util}% full. An email has been sent to your administrator.
      mail -s "Critical: $part is ${util}% full. Clear space immeadiately!" $admin
    fi
done

Output:

/dev/mapper/rootvg-root is 92% full. An email has been sent to your administrator.

But, you can see /dev/mapper/rootvg-apps is also 98% full:

/dev/mapper/rootvg-apps            20G   20G  445M  98% /apps
/dev/mapper/rootvg-tmp            6.0G  2.4G  3.7G  39% /tmp
/dev/mapper/rootvg-var_log        6.0G   71M  6.0G   2% /var/log
/dev/xvda1                        497M  142M  356M  29% /boot

marked as duplicate by Jeff Schaller, Stephen Kitt, mdpc, Stephen Harris, Thomas Dickey Sep 1 '16 at 1:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    I suspect your mail command reads from stdin and messes up things. (Google for "bash while loop stops after first iteration"). What version/package of mail is that? – Mat Aug 31 '16 at 14:20
  • Name:mailx Arch:x86_64 Version:12.5 Release:12.el7_0 I think you're on to something. When I removed the mail command, both lines were printed. However, the mail component is a crucial part of the script. – N.M.D Aug 31 '16 at 14:42
  • add echo $i | before the mail command. (you might replace $i with a more meaningfull message) – Archemar Aug 31 '16 at 14:52
  • @Archemar This worked. Can you please help to explain why. – N.M.D Sep 1 '16 at 14:25
  • first output line of df is feeded to while read as mail expect value from STDIN it read the rest. my suggest edit provide a STDIN to mail. – Archemar Sep 1 '16 at 14:43
0

I think problem with your df -h output.

Try with df -Ph

Update:

As per man page, -P refers POSIX output format

-P, --portability
    use the POSIX output format

And, if your partitions having more length and while doing text processing with your command (without -P) your expected line output will be coming in two lines.

Expectation (Example):

/dev/mapper/rootvg-tmp/tmp1   86
/dev/mapper/rootvg-tmp/tmp2   90
/tmp   90

Actual Output (Example):

/dev/mapper/rootvg-tmp/tmp1
86
/dev/mapper/rootvg-tmp/tmp2
90
/tmp   90

So, in this case your condition in the script will not match for first two partitions since '{print $2}' will be none and $util -ge $threshold will be false.

HTH

  • I will update my answer with details. – Spike Sep 1 '16 at 3:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.