?
Solved

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

Posted on 2009-05-08
5
Medium Priority
?
581 Views
Last Modified: 2013-12-27
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
Comment
Question by:sunhux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 800 total points
ID: 24343987
#! /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
 
LVL 4

Assisted Solution

by:docbert
docbert earned 400 total points
ID: 24618196
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
 
LVL 26

Assisted Solution

by:arober11
arober11 earned 800 total points
ID: 25158678
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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 800 total points
ID: 25159181
> As the following are equivalent:
no, except you mean that a vixie cron is equivalent to a vixie cron ;-)
0
 
LVL 26

Assisted Solution

by:arober11
arober11 earned 800 total points
ID: 25160061
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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question