• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

Sun Solaris Unix Shell script that adds 2 to a string numeric and checks last value for change before emailing

Currently our Sun E450 could detect room temperature (from "prtdiag -v" and I've
observed that it's consistently reporting about 1.5-2.0 Celsius lower than the
general DC room temperature :

# prtdiag -v | grep -i ambie
AMBIENT    19

"date" command in this Solaris box's format is :
# date
Sat May  9 06:06:58 SGT 2009


I'll need a Shell script that run every 1 minute (I'll put this into the crontab as
1,2,...59 * * * * /path/script_name) that will do the following :

I'll need this Shell script to be able to notify/email me when room temperature
(>= 21  AND there's a change from the last value)   or
(<= 20  AND the temperature recorded one minute ago is >= 21)

Above script basically notifies me once only (not repeatedly) when temperature
hits 21 C  but if temperature keep going up (at levels above 21 C) it will keep emailing/
notifying me.
This script only notify me once when temperature go below 21 C (but only notify
once and not repeatedly and it will stop notifying if room temperature stays below
20 C)  as the pseudo codes below describe :

Currentdatetime=yyyymmdd:hhmm   (24 hr format)
echo RoomTemp + " " + Currentdatetime  >> yyyymmdd_outputfile
add 2 to the value given by "prtdiag -v | grep AMBIENT" (call this variable RoomTemp
  if RoomTemp equals or exceeds 21
    check RoomTemp vs last_temperature recorded in last line of yyyymmdd_outputfile
        if RoomTemp equal or exceed 21 AND is higher than last_temperature
           email to myemail the value Currentdatetime+" "+":Room temp is now "+RoomTemp
        endif
  if last_temperature in last line of yyyymmdd_outputfile >= 21 and RoomTemp is <=20
    email to myemail the value Currentdatetime+" "+":Room temp now is down to "+RoomTemp
  endif
exit

0
sunhux
Asked:
sunhux
  • 2
  • 2
5 Solutions
 
ahoffmannCommented:
#! /bin/sh
last_temp=0
last=/tmp/last_temp
[ -e $last ] &&  last_temp=`cat $last`
temp=`prtdiag -v |awk '/AMBIENT/{print $2}'`
echo $tmp >$last
[ $temp -ge 21 ] && mail -s21+ me@some.where
[ $temp -le 20 -o $last -ge 21 ] && mail -slast21+ me@some.where
exit 0
# not that the script relies on your cron to satisify your "one minute ago" condition
0
 
docbertCommented:
ahoffmann's script misses one of your requirements - that it only sends a mail when "there's a change from the last value". As it is it will continue to email you once a minute until the temperature drops.

You can add the following below the "echo" statement to add that requirement :

[ $temp -eq $last_temp ] && exit

The 2nd last line is also wrong - it's using a -o (or) where it should be and, plus it's using $last rather than $last_temp.  I'd replace this line with :

[ $temp -le 20 ] && [ $last_temp -ge 21 ] && mail -slast21+ me@some.where
0
 
arober11Commented:
On the crontab front, you dont need:

1,2,...59 * * * * /path/script_name

As the following are equivalent:

0-59 * * * * /path/script_name

Or:
* * * * * /path/script_name

0
 
ahoffmannCommented:
> As the following are equivalent:
no, except you mean that a vixie cron is equivalent to a vixie cron ;-)
0
 
arober11Commented:
Well yes, using either variant above, assuming your using a cron daemon that supports Paul Vixie 1987 syntax, will save having to add an entry along the lines :

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,34,35,36,37,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 * * * * /path/script_name


You might also want to look at redirecting the output from the cron, to save the output being mailed to owning user, by appending something like this to the line:

 >/var/log/some_log_file 2>&1
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now