0

I have a bash script that checks the temperature of an external thermometer and sends an alert if it drops below a defined amount. However what it does is send the alert constantly after the 10 second period (this is how it was originally designed). What i'd like it to do is if it drops below 22 degrees send the alert once and if it goes above 22 degrees send another alert but again only once and so on.

#!/bin/bash

 while true; do
     val=$(cat /sys/bus/) 
     min=$(echo "22.0")
     if [[ "$val" < "$min" ]]
     then
         sendmail "too cold"
         sleep 10

     else
         echo "All ok"
         sleep 10
     fi
 done
 exit 0
2
  • you can do it storing val in some temp variable and check only for follwing condition like if [[ "$prev" -ne "$val" ]];then prev=$val if [[ "$val" < "$min" ]];then ..... Mar 25, 2014 at 8:26
  • min=22.0 is sufficient; you don't need to capture the output of echo.
    – chepner
    Mar 28, 2014 at 20:04

3 Answers 3

2

First I wouls take out the sleep 10 from the place you put it and put in outside de if statements. The the problem comes because you are executing you script in an infinitie loop which means that it will be executed one time and another.

You can always create a file using touch in a specified directory and check if it exists:

#!/bin/bash

while true; do
  val=$(cat /sys/bus/) #removed a ' typo
  min=$(echo "22.0")

  if [[ "$val" < "$min" ]]; then
    if [ ! -f "/var/testdir/message1_sent" ]; then
      sendmail "too cold"
      touch /var/testdir/message1_sent

      if [ -f "/var/testdir/message2_sent" ]; then 
        rm /var/testdir/message2_sent
      fi
    fi

  else

    if [ ! -f "/var/testdir/message2_sent" ]; then
      echo "All ok"
      touch /var/testdir/message2_sent

      if [ -f "/var/testdir/message1_sent" ]; then 
        rm /var/testdir/message1_sent
      fi

    fi

  fi

  sleep 10

done
#exit 0 #This is not reached, you are always inside a loop
8
  • thank you for your help, it's looking good but I tried replacing echo "All ok" with sendemail "Just right" but I get it twice every 10 seconds
    – Lurch
    Mar 25, 2014 at 20:31
  • 1
    Ok I'll check it and let you now, stay close ^__^
    – ederollora
    Mar 25, 2014 at 20:43
  • you must have typed something wrong. I copy-pasted the code and change val to be read from keyboard and it works
    – ederollora
    Mar 25, 2014 at 20:52
  • be sure of change directory in if statements and also at the time of doing touch
    – ederollora
    Mar 25, 2014 at 20:52
  • Hi thanks for that, however what I found is that when I started it when it was <22 I got echo 'too cold' and 'message1_sent' was created but when it went to 22> I got the message 'all ok' and message2_sent was created but message1_sent still remained so when it went to <22 again I didn't get an alert, so it needs to delete a file on creation of the other some how?
    – Lurch
    Mar 28, 2014 at 19:40
0

I guess that it sends mails again because the temperature is not over 22.0 degrees (it may take time to get over 22.0 degrees?). You could add a flag that is sent when the email is sent and then only enter in the val < min branch if the flag is not set. When the temperature is established over 22.0 degrees again, the flag should be cleared and then everything should work again.

0

You can always use an extra variable to signal that you already sent the notification for too cold (<22) and another one for Ok (>22). These are initially false, when you first send the "< 22" notification you flip the variable for <22 to true and the one for >22 to false, and of course modify the script to send only the notification if your variable is false. When you send the "All ok" notification you again flip the variable for <22 to false and for >22 to true and this should do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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