Test temperature and alert

i have this script which result is cpu temp in celsius

first install:

apt-get install lm-sensors
YES |sensors-detect --auto

#!/bin/bash

# 1. get temperature

## a. split response
## Core 0:       +143.6�F  (high = +186.8�F, crit = +212.0�F)
IFS=')' read -ra core_temp_arr <<< $(sensors -f | grep '^Core\s[[:digit:]]\+:') #echo "${core_temp_arr[0]}"

## b. find cpu usage
total_cpu_temp=0
index=0
for i in "${core_temp_arr[@]}"; do :
    temp=$(echo $i | sed -n 's/�F.*//; s/.*[+-]//; p; q')
    let index++
    total_cpu_temp=$(echo "$total_cpu_temp + $temp" | bc)
done
avg_cpu_temp=$(echo "scale=2; $total_cpu_temp / $index" | bc)

## c. build entry
temp_status="CPU: $avg_cpu_temp F"
#echo $avg_cpu_temp
#echo $[(${avg_cpu_temp}*9/5)+32]
tc=$(echo "scale=2;(5/9)*($avg_cpu_temp-32)"|bc)
echo $tc=$avg_cpu_temp

this give a number like: 54

I want to have some condition, possible in new .sh and test if its ok or not.
For the start I want to tell temp is OK or not, just echo. Then i will figure out how to send to email.
I have something similar but for raspberry and mail function, but here on ubuntu i don't figure out just yet.

Hi,

If all you're looking for is a way in Bash to test the value of a number, then the if statement can handle that on its own.

For example, here's a script that will test a previously-defined number, and display one message if it's less than or equal to 90 (the meaning of the -le arithmetic operator), and a different message if any other condition is met (i.e. if it's greater than 90):

$ cat example.sh
#!/bin/bash
randomnumber=`/usr/bin/shuf -i 1-100 -n 1`
echo "Considering: $randomnumber"

if [ "$randomnumber" -le "90" ]
then
        echo "Less than or equal to 90, so that's OK"
        exit 0
else
        echo "Oh no, it's greater than 90 !"
        exit 1
fi

Here's the output of a sample session:

$ ./example.sh 
Considering: 5
Less than or equal to 90, so that's OK
$ ./example.sh 
Considering: 91
Oh no, it's greater than 90 !
$ ./example.sh 
Considering: 27
Less than or equal to 90, so that's OK
$

So if all you're looking for is "If less than X do this, if greater than X do the other" the if statement is probably all you need. Hope this helps.

Insstead if your random number how can i put a result of my bash script?

Hi,

I'd just included that as a demonstration of how if works, really, so you could get an idea of what you need to do in your own code. So rather than directly using my example script, you'd adapt it and write your own if statement in your script to test the value of your temperature variable, after your script had set it.

So looking at your provided code, your could write an if statement at the end to test the value of the tc variable (assuming that's correct, and it does contain just a single integer that is the number you want to test). If the variable tc isnt' the right one then you'd use whatever variable contains the CPU temperature as an integer, and do the if tests on that.

If that doesn't work then if you can provide an example of the code you've written and what goes wrong when you run it, I'd be happy to help further in debugging and we can take things from there.

It gives me

this is a part of code

tc=$(echo "scale=2;(5/9)*($avg_cpu_temp-32)"|bc)
echo $tc
if [ "tc" -le "90" ]
then
        echo "Less than or equal to 90, so that's OK"
        exit 0
else
        echo "Oh no, it's greater than 90 !"
        exit 1
fi

didn't you mean?

if [ "${tc}" -le  90 ]

Hi,

Aside from missing out the $ before your variable name as has been pointed out, the other problem you'll have here is that you have to cast your temperature variable (which is a floating-point number) into an integer. Bash built-in arithmetic of the kind we're using here solely works with integers, and not floats.

There are many ways you could do that - off the top of my head, you could get an external integer calculator (such as bc ) to divide it by one, which will have the effect of trimming off the decimal part.

For example:

#!/bin/bash

temperature=44.55
integer=`echo "$temperature / 1" | /usr/bin/bc`

echo "Before conversion: $temperature"
echo "After conversion: $integer"

if [ "$integer" -le "90" ]
then
        echo "Less than 90, so that's OK"
        exit 0
else
        echo "Oh no, it's at least 90 !"
        exit 1
fi

So here I start with a hard-coded floating point value for temperature , get bc running in its default integer-only mode to divide it by one, assign that result to a variable called integer , and do my Bash arithmetic operators on that integer.

Here's an example session.

$ ./example.sh 
Before conversion: 44.55
After conversion: 44
Less than 90, so that's OK
$

Hope this helps.

Can i somehow use this result variable to send a mail? Which mail client do you propose

A bit simpler conversion:

integer=`printf "%d" "$temperature"`

The %d rounds the following argument to integer and echoes it.
The most simple way to send mail

echo "hello" | mail recipient@domain

All in one:

printf "CPU temp %d F\n" "$temperature" | mail recipient@domain

Most mail programs take a -s "subject" option.

i can send it manually, but when add to test it and add with crontab -e

          • bash /home/user/temp

nothing happend.
WHy? where am i wrong?

this last remark may be, say, a wee bit imprecise. If it were true, you should check your cron installation / status.
Should, in contrary, cron be installed (and running) correctly, you should have at least one syslog entry per minute. And, should /home/user/temp be the script we are talking of in this thread, its standard and error output - given a mail daemon is running on your system - would be mailed to you, unless you modified the mail recipient in another crontab entry. Not withstanding the e-mail you explicitly send to somewhere in some domain.

How COULD we tell, with nothing but a single line crontab entry with unspecific data at hand? Why not post WAY more details of your problem?

Please run manually

/usr/bin/env PATH=/bin:/usr/bin bash /home/user/temp

The PATH is set like from crontab.
Does it work?