Limiting the Script

Greetings.

I have script to monitor the disk space of folder it runs every 17 min with help of cron. It sends email when disk size reaches to 85 %. Now the issue is that it continousely generates email until we clear some space in that folder. Is it possible to restrict the Script to send only one alert per an hour even though the cron runs 17 min.

I can use the Disk Quota for this but we dont have permission to enable
only we can monitor the disk space with script only

Why 17 minutes, not 15?

One way of doing this is to keep track of the time in a file. Just to give you an idea, you can code something like this:-

CURR_TS=$( date +"%Y%m%d%H" )                       # Current time-stamp
FILE_TS=$( cat curr_ts.txt 2> /dev/null )           # Time-stamp in file

if [ $CURR_TS -eq $FILE_TS ]                        # Checking if current time-stamp & time-stamp in file is matching.
then
      # Do not send email
else
      # Do send email
      echo $CURR_TS > curr_ts.txt                   # Update new time-stamp in file.
fi

concept doesn't work when services is having issue. Just need to limit the alerts to send only one mail per hour instaed of sending multiple emails

Can you explain why you think that this concept does not work in limiting one mail per hour?

cron is running every 17 min, For eg;-if the /apps folder size is reached 85 % in within hour every 17 minutes the alert will generate unless we manually clear some space.

Understood. If you look at the code I suggested above, it is getting the time-stamp with date and hour CURR_TS=$( date +"%Y%m%d%H" ) for each run and writing it to a file only if an alert was send. So next time if the script run within the same hour & try to send an alert, the condition will fail because the CURR_TS & FILE_TS are same and an alert will not be send. Do you still think that this concept does not work?

Issue is that it Compares the Hours each time when the script runs
for eg., at 2:00 PM the script runs first it will store the 2012120702
and when script runs 2nd time again 2012120702 so the script will
generate the email because last hours also 2 and this time also 2

Below is the script which i tested

#!/bin/bash
set -xv
#Current Time Stamp
CURR_TS=$( date +"%Y%m%d%H" )
FILE_TS=$( cat /tmp/curr_ts.txt 2> /dev/null ) # Time stamp in file
#admin email account
ADMIN=abc@test.com
# set usage alert threshold
THRESHOLD=60
#hostname
HOSTNAME=$(hostname)
#mail client
MAIL=/bin/mail
# store all disk info here
EMAIL=""
for line in $(df -hP | egrep '^/dev/' | awk '{ print $6 "_:_" $5 }')
do
        part=$(echo "$line" | awk -F"_:_" '{ print $1 }')
        part_usage=$(echo "$line" | awk -F"_:_" '{ print $2 }' | cut -d'%' -f1 )
        if [ $part_usage -ge $THRESHOLD -a -z "$EMAIL" ];
        then
                EMAIL="$(date): Running out of diskspace on $HOSTNAME\n"
                EMAIL="$EMAIL\n$part ($part_usage%) >= (Threshold = $THRESHOLD%)"
        elif [ $part_usage -ge $THRESHOLD ];
        then
                EMAIL="$EMAIL\n$part ($part_usage%) >= (Threshold = $THRESHOLD%)"
        fi
done
if [ -n "$EMAIL" ] && [ "$CURR_TS" -eq "$FILE_TS" ];
then
        echo -e "$EMAIL" | $MAIL -s "Alert: Partition(s) almost out of diskspace on $HOSTNAME" "$ADMIN"
 else
       echo $CURR_TS > /tmp/curr_ts.txt;
fi
 

I said to send email only if the CURR_TS and FILE_TS is not matching, so perform the below correction and re-try:-

if [ -n "$EMAIL" ] && [ "$CURR_TS" -ne "$FILE_TS" ];

No luck still the mails are going when i run the script 2nd and 3rd time also:(

I have 2 questions for you

  1. Why do you have 2 shebangs in your script!
#!/bin/bash
set -xv
#!/bin/sh
  1. Why you are not using absolute path for curr_ts.txt file here?
echo $CURR_TS > curr_ts.txt; # replace it with /tmp/curr_ts.txt

Step 1. I accidently put that i removed it.
Step 2. Sorry, I have included the path.

Now, Once again I run the script twice still the same issue

OK.
Set the debug and see what is going on.
Echo the values of $CURR_TS and $FILE_TS right before the final if condition.
Check the values and check the if condition execution flow.

You havent answered my first question yet : why 17 and not 15 minutes? This question is fundamental since you re wanting an hourly mail

The current interval is set to 15 min as i understood it will complete the cycle of generating only 4 emails per hour.