I want to make a script in my bashrc to tell me what my CPU temp is, IF it is over 60C. If it is under that, I want it to say nothing. So far I have this:

if [ "$STR | grep high" -gt "temp1:        +40.1°C  (high = +70.0°C)" ]
    exec $STR | grep "high"

Help? I plan to add this to my .bashrc so it notifies if my CPU temp is high when I open the terminal.

Update: output from sensors:

Adapter: PCI adapter
temp1:        +40.0°C  (crit = +120.0°C, hyst = +90.0°C)

Adapter: PCI adapter
temp1:        +40.5°C  (high = +70.0°C)
                       (crit = +100.0°C, hyst = +97.0°C)
  • Please add the output of sensors so that we can see how it looks like and what adapters you have. – Kev Inski Sep 28 '16 at 7:45
  • Did you run sudo sensors-detect? None of your two sensors is your CPU. Both are PCI-Adapters! – Kev Inski Sep 29 '16 at 7:22
  • Yes. Also, not only do I have no PCI adapters, but there is not even an area to add them on my mobo. So no... they aren't PCI adapters. Why it says that, though, is beyond me – Freal Ackman Oct 6 '16 at 13:37
  • That's strange, maybe it is just the PCI-Bus, but who know. The k10temp is your CPU, but it is inaccurate. See this post: stackoverflow.com/questions/31421874/… . Tomorrow I have a little bit time and can check to see if I can get you a working script. – Kev Inski Oct 6 '16 at 18:31

This is an improvement on Kev Inski's answer:

# Display CPU temperature, if it is above the "high" threshold.

# Desired adapter.

# Extract given temperature from `sensors`.
    sensors -uA "$adapter" |
        grep "$1" |
        cut -d. -f1 |
        grep -oE '[0-9]+$'

# Get current temperature.
temp1_input=$( get_temp 'temp1_input' )

# Get high temperature.
temp1_high=$( get_temp 'temp1_high' )

# Compare current temp against high.
if [[ $temp1_input -ge $temp1_high ]]; then
    echo "Your CPU is ${temp1_input}°C"

Why yours won't work:

[ "$STR | grep high" -gt "temp1:        +40.1°C  (high = +70.0°C)" ]

This is comparing two strings, but telling Bash to treat them as integers, hence why the error is "integer expression expected". You need to pare down the string to just the numbers. (See my function above.) And anyway, "$STR | grep high" won't execute until you put it in a command substitution like $(...).

exec $STR | grep "high"

Don't execute a program using exec unless you need to. Also, quote your variables!


To get the output of an executed command you need to use backticks (`) otherwise you only get the return value of that command.

I don't really know what you think your code does, but I do understand what you want it to do. I wrote a short script that does what you want. It is not very flexible and I am not good with REGEX, awk and sed.

So this is what I did:


# Change the adapter to your desired one

# Extract the high temp from the string
HIGH_TEMP=`sensors | grep -A5 "${ADAPTER}" | grep temp1 | awk '{print $5}' | sed -e 's/+//' -e 's/\..°.*//'`

# Extract the current temp from the string and print it
CURR_TEMP=`sensors | grep -A5 "${ADAPTER}" | grep temp1 | awk '{print $2}' | sed -e 's/+//' -e 's/\..°.*//'`
echo "Current temperature: ${CURR_TEMP}°C"

# check if current temp is greater or equal 
if [ "${CURR_TEMP}" -ge "${HIGH_TEMP}" ]; then
   echo "Do Something to keep cool."

As I said it does the work but not in a very good way. There are most likely a shorter one liner for getting the temperatures.

I forgot: You can save that script and grant execution permission

$ chmod u+x /PATH/TO/SCRIPT/sciptname.sh

And place the /PATH/TO/SCRIPT/sciptname.sh to your .bashrc

  • Thanks a lot for the help, truly. I'm getting this error which is one of the same error codes I kept getting when I was trying to complete it on my own, playing around and trying different things from what I saw in Google for hours. /home/solas/Scripts/welcome: line 36: [: : integer expression expected – Freal Ackman Sep 28 '16 at 18:30
  • That's why I asked you to put your sensors output into your question. The PC I was on yesterday had an output like that: Physical id 0: temp°C (high = ...°C ...) It was in one line. Your output is not in one line, so the vars HIGH_TEMP and CURR_TEMP are empty. – Kev Inski Sep 29 '16 at 7:19
  • I changed the script and used your sensors-output for it so I hope it will work now. – Kev Inski Oct 6 '16 at 19:21
  • Thank you lots... Blame the late reply on Hurricane Matthew. I've been out of power for days – Freal Ackman Oct 10 '16 at 12:35
  • 1
    Backticks are deprecated. Use $(...) because it's more flexible, e.g. you can nest expressions. – wjandrea Nov 4 '16 at 6:00

I've written a monitoring script for sensors once. Below is part of the script that you want:

sensors | awk '/temp1/ {if (substr($2,2,2)>60) print "Temperature over 60 degrees"}'

This line can be enclosed into a bash function and placed into your ~/.bashrc if you wish to run it as single command on demand. In your sensors output there are two temp1 fields so it will report if any of them are over 60. You could modify the code to print which adapter has high temperature, like so:

sensors | awk '/Adapter/{adp=$0}/temp1/ {if (substr($2,2,2)>60) print adp ": Temperature over 60 degrees"}' 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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