Hi again,
I think I found the answer myself, thank Google and
man shutdown
Looks like
shutdown must be run as root only, so I assigned the script in Webmin to root.
Also, when I tested the script, I found out the following:
1. If CPU temperature reaches warning limit, the script nevertheless reports at the end that both cores are OK, not sure if it was a bug or a feature, but I wanted to change that.
2. When I test-run the script as a cron job in Webmin, I would always get a message "Bad substitution". Turned out the very first line of each script should be
#!/bin/bash, but I did not copy it

When the line was added, the message dissapeared.
3. after
shutdown now command e-mail will not be sent. Actually nothing will be done (at least on my computer). I tried to add time delay as
shutdown +1 which suppose to shutdown in 1 minute. However, it did not help. The server indeed would switch itself off in 1 minute, but no other commands after
shutdown would be executed. So I moved
shutdown to be last command
after sending an e-mail.
4. I wanted the script to send me e-mail also in case temperature reaches a
warning limit, not just record that in a log.
5. Also I thought it might be a good idea if the message that is sent to me would contain info about when the temperature was high, and what the temperature actually was.
So, I changed the script a bit

to do all that stuff:
Code: Select all
#!/bin/bash
# PURPOSE: Script to check temperature of CPU cores and report/shutdown if spe$
#
# AUTHOR: feedback[AT]HaveTheKnowHow[DOT]com; modified by anp :)
# Expects two arguments:
# 1. Warning temperature
# 2. Critical shutdown temperature
# eg. using ./CPUTempShutdown.sh 30 40
# will warn when temperature of one or more cores hit 30degrees and shu$
# NOTES:
# Change the strings ">>/home/htkh" as required
# Substitute string "myemail@myaddress.com" with your own email address in the$
# Assumes output from sensors command is as follows:
#
# coretemp-isa-0000
# Adapter: ISA adapter
# Core 0: +35.0 C (high = +78.0 C, crit = +100.0 C)
#
# coretemp-isa-0001
# Adapter: ISA adapter
# Core 1: +35.0 C (high = +78.0 C, crit = +100.0 C)
#
# if not then modify the commands str=$(sensors | grep "Core $i:") & newstr=${$
echo "JOB RUN AT $(date)"
echo "======================================="
echo ''
echo 'CPU Warning Limit set to => '$1
echo 'CPU Shutdown Limit set to => '$2
echo ''
echo ''
#sensors
echo ''
echo ''
for i in 0 1
do
str=$(sensors | grep "Core $i:") #get string with Core from sensors
newstr=${str:15:2} #truncate the string to first two digits of temperature
#First check if the temperature is critical.
if [ ${newstr} -ge $2 ]
then
echo '============================'
echo ''
echo 'CRITICAL: TEMPERATURE CORE' $i 'EXCEEDED' $2 '=>' $newstr
echo ''
echo '============================'
#
#Creating message:
echo 'From: YourMail@gmail.com' >/home/USER_NAME/MyScripts/msg.txt
echo 'Subject: CRITICAL server temperature' >>/home/USER_NAME/MyScripts/msg.txt
echo '' >>/home/USER_NAME/MyScripts/msg.txt
echo "CPU core "$i" reached critical temperature of "$newstr" at $(date). Server was shut down." >>/home/USER_NAME/MyScripts/msg.txt
/usr/sbin/ssmtp YourMail@gmail.com </home/USER_NAME/MyScripts/msg.txt
echo 'Email Sent.....'
/sbin/shutdown -P now
exit
#Now check if the temperature just exeeds normal
elif [ ${newstr} -ge $1 ]
then
echo 'From: YourMail@gmail.com' >/home/USER_NAME/MyScripts/msg.txt
echo 'Subject: Warning Server temperature' >>/home/USER_NAME/MyScripts/msg$
echo '' >>/home/USER_NAME/MyScripts/msg.txt
echo "CPU core "$i" reached high temperature of "$newstr" at $(date)." >>/home/USER_NAME/MyScripts/msg.txt
/usr/sbin/ssmtp YourMail@gmail.com </home/USER_NAME/MyScripts/msg.txt
echo 'Email Sent.....'
else
#OK, temperature within normal limit
echo ' Temperature Core '$i' OK at =>' $newstr
echo ''
fi
done
echo 'Script completed'
echo ''