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
shutdown
. And/or try putting it in/etc/crontab
. And/or try usingpoweroff
instead ofshutdown
.