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
cpu=$(</sys/class/thermal/thermal_zone0/temp); [ "$cpu" -gt 70000 ] && shutdown
[ "$(</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
$(cat ...) or you may set
SHELL=/bin/bash at the top of the crontab. Also, to run
shutdown, you likely need root privileges.