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
578 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
  • 2
  • 2
5 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 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 100 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 200 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 200 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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

829 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