2

Background

I'm trying to write a script (see end of the question) that checks the temperature on Raspberry PI and shuts it down automatically if it goes too high. The script would be run by cron, once a minute, as root.

The problem

The script is executed, the message is printed as expected, but the shutdown is not scheduled, and there is no error message about why. Indeed after cron runs the script, cat /run/systemd/shutdown/scheduled shows nothing (file does not exist). However, after running the script manually, (still as root), cat /run/systemd/shutdown/scheduled shows that the shutdown is indeed scheduled.

Question

Why does the script work, when invoked manually, but not when invoked by cron (even though it is in root's crontab)?

Related

This question is similar, but the conclusion of the answers seems to be that adding shutdown to root's crontab (via crontab -e as I do) should be fine.

Details

The script: /root/shutdown_overheat.sh

#!/bin/sh

MAXTEMP=30         # temporarily lowered for testing
TEMP=$(/opt/vc/bin/vcgencmd measure_temp | awk -F'[=.]' '{print $2}')
MSG="Temperature $TEMP higher than $MAXTEMP, shutting down in 2 minutes"

if [ "$TEMP" -gt "$MAXTEMP" ]; then
        wall $MSG
        logger System $MSG
        shutdown -h +2 $MSG
fi

Running the script manually (as root):

# ./shutdown_overheat.sh 

Broadcast message from attilio@kolcsag (pts/0) (Fri Mar 13 20:41:13 2020):     

Temperature 54 higher than 30, shutting down in 2 minutes

Shutdown scheduled for Fri 2020-03-13 20:43:13 GMT, use 'shutdown -c' to cancel.
# cat /run/systemd/shutdown/scheduled 
USEC=1584132193792504
WARN_WALL=1
MODE=poweroff
WALL_MESSAGE=Temperature 54 higher than 30, shutting down in 2 minutes

Script executed by cron:


Broadcast message from root@kolcsag (somewhere) (Fri Mar 13 20:42:01 2020):    

Temperature 54 higher than 30, shutting down in 2 minutes

# cat /run/systemd/shutdown/scheduled 
cat: /run/systemd/shutdown/scheduled: No such file or directory

Root's crontab:

# crontab -l



# Edit this file to introduce tasks to be run by cron.
# 
# ... etc

* * * * * /root/shutdown_overheat.sh
  • Any errors emailed to root by the cron daemon? – Kusalananda Mar 13 at 21:16
  • I don't know: where can I check that? – Attilio Mar 13 at 21:19
  • In root's mailbox? – Kusalananda Mar 13 at 23:07
  • it could be a path issue, try specifying the full path to shutdown. And/or try putting it in /etc/crontab. And/or try using poweroff instead of shutdown. – gogoud Mar 14 at 19:58

Your Answer

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

Browse other questions tagged or ask your own question.