I am not sure if this is even possible directly. But I am trying to get the current temperature of the CPU using:


I want to pipe the output of the same command using echo to a if statement to carry out a shutdown if the temperature is higher then 70000. (Output of the command needs to be divided by 1000 to temperature in centigrade).

something like

cpu=$(</sys/class/thermal/thermal_zone0/temp) | if $cpu -gt 70000 then shutdown fi

I am trying to do this to stop my pi from overheating. I will schedule a cronjob that checks every 15 minutes for CPU temperature of my Pi and shut it down if it is over 70.

Thank you.


Your last command is almost correct, but the syntax is wrong. Corrected version:

cpu=$(</sys/class/thermal/thermal_zone0/temp); if [ "$cpu" -gt 70000 ]; then shutdown; fi

Or, shorter,

cpu=$(</sys/class/thermal/thermal_zone0/temp); [ "$cpu" -gt 70000 ] && shutdown

Or, shorter,

[ "$(</sys/class/thermal/thermal_zone0/temp)" -gt 70000 ] && shutdown

The issue with your command (apart from the syntax of the if-statement) is that you use a pipeline. The right hand side of a pipeline (your if statement) should read from the left hand side of the pipeline (your variable assignment). A variable assignment does not produce any output, and your if-statement does not consume any input. Furthermore, the value of $cpu is unknown on the right hand side of the pipe as the two parts of the pipeline are executed concurrently.

For a cron job, the following may be a better formulation of the command:

[ "$(</sys/class/thermal/thermal_zone0/temp)" -le 70000 ] || shutdown

This ensures that the command exits successfully even if the temperature is low. The [ ... -gt 70000 ] && shutdown variation would return a non-zero exit from the [ ... ] test for low temperatures, which would make cron think the job failed.

Note also that this requires that the crontab is using bash as the shell to run the jobs, or the $(<...) syntax may not work properly. You may change $(<...) to $(cat ...) or you may set SHELL=/bin/bash at the top of the crontab. Also, to run shutdown, you likely need root privileges.

  • Hey I truly appreciate this. But it does not seem to work in the crontab. Here is the grep of the syslog: Oct 30 00:22:02 hostname CRON[8298]: (user1) CMD ([ "$(</sys/class/thermal/thermal_zone0/temp)" -le 7000 ] || shutdown now) Oct 30 00:22:05 hostname CRON[8294]: (user1) MAIL (mailed 64 bytes of output but got status 0x0001 from MTA#012) – Parth Maniar Oct 29 '18 at 19:05
  • I tried this with sudo crontab -e also to launch it as root but doesn't work. – Parth Maniar Oct 29 '18 at 19:27
  • @ParthManiar See added bit at the end. Let me know what the email to root says if it fails. – Kusalananda Oct 29 '18 at 19:59
  • Thank you very much. I did it with sudo poweroff in crontab of root and it worked. I do see an error after the first half of the command which is the output of temperature digits. I want to run multiple tasks: send an email before shutting down, stopping services and unmount drives before shutting down. Something like I am testing with 7000 as it is below the current temperature. [ "$(</sys/class/thermal/thermal_zone0/temp)" -le 7000 ] || /path_to_bash_script/1 >> /path_to_file/1.txt || cat /path_to_file/1.txt | mail -s "CRITICAL_SHUTDOWN_INITIATED" name@email.com || sudo poweroff – Parth Maniar Oct 29 '18 at 20:30
  • @ParthManiar I would suggest that you put everything in a script by itself and schedule that script with crontab. That would make it much easier. Scheduling things that are only one or two commands directly with crontab is ok, but as soon as you have three or more commands and some kind of conditional execution of things, then it's very much easier to just write that in a shell script and schedule that instead. This would make it easier to maintain the script without bothering about crontab. Also, if you run the cron job as root, you won't need sudo. – Kusalananda Oct 29 '18 at 20:59

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.