这是我尝试制作一个可以睡眠的脚本:
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 ''
stop=0
while(true)
do
sleep 1.5
str=$(sensors | grep "Core $i:")
newstr=${str:14:2}
if [ ${newstr} -ge $1 ]
then
echo '============================' >>/home/xybrek/Desktop/CPUWarning.Log
echo $(date) >>/home/xybrek/Desktop/CPUWarning.Log
echo '' >>/home/xybrek/Desktop/CPUWarning.Log
echo ' WARNING: TEMPERATURE CORE' $i 'EXCEEDED' $1 '=>' $newstr >>/home/xybrek/Desktop/CPUWarning.Log
echo '' >>/home/xybrek/Desktop/CPUWarning.Log
echo '============================' >>/home/xybrek/Desktop/CPUWarning.Log
fi
if [ ${newstr} -ge $2 ]
then
echo '============================'
echo ''
echo 'CRITICAL: TEMPERATURE CORE' $i 'EXCEEDED' $2 '=>' $newstr
echo ''
echo '============================'
/sbin/pm-suspend
echo 'Sleeping....'
exit
else
echo ' Temperature Core '$i' OK at =>' $newstr
echo ''
fi
done
echo 'Both CPU Cores are within limits'
echo ''
当我运行脚本时,它每1.5秒循环一次,但不显示newStr。它是空的。该脚本的基本思想是使PC“睡眠”。当温度达到一定水平时。
答案 0 :(得分:0)
$i
未定义,如您所示的代码所示。
如果$i
为空,$str
将为空。 $newstr
是$ str的子字符串,带有错误的索引btw。
所以$newstr
将为空。
控制台测试:
str@s131-i:~> str=$(sensors | grep "Core 0:")
str@s131-i:~> echo $str
Core 0: +33.0°C (high = +74.0°C, crit = +100.0°C)
str@s131-i:~> newstr=${str:14:2}
str@s131-i:~> echo $newstr
+3
str@s131-i:~> str=$(sensors | grep "Core :")
str@s131-i:~> echo $str
str@s131-i:~>
再编辑一条评论:
这个脚本不是很健壮。如果参数丢失,它将使您的系统进入睡眠状态。我宁愿使用一些脚本语言,如果你部署到几个系统使用一个没有额外的库或工具来完成这项工作。 bash语法有点......好吧......
答案 1 :(得分:0)
这对我有用:
#!/bin/bash
# PURPOSE: Script to check temperature of CPU cores and report/shutdown if specified temperatures exceeded
#
# AUTHOR: feedback[AT]HaveTheKnowHow[DOT]com
# 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 shutdown when either hits 40degrees.
# NOTES:
# Change the strings ">>/home/xybrek" as required
# Substitute string "myemail@myaddress.com" with your own email address in the string which starts "/usr/sbin/ssmtp myemail@myaddress.com"
# 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=${str:14:2} below accordingly
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 ''
stop=0
while true;
do
sleep 1.5
for i in 0 1
do
str=$(sensors | grep "Core $i:")
newstr=${str:17:2}
if [[ ${newstr} -ge $1 ]]
then
echo '============================' >>/home/xybrek/Desktop/CPUWarning.Log
echo $(date) >>/home/xybrek/Desktop/CPUWarning.Log
echo '' >>/home/xybrek/Desktop/CPUWarning.Log
echo ' WARNING: TEMPERATURE CORE' $i 'EXCEEDED' $1 '=>' $newstr >>/home/xybrek/Desktop/CPUWarning.Log
echo '' >>/home/xybrek/Desktop/CPUWarning.Log
echo '============================' >>/home/xybrek/Desktop/CPUWarning.Log
fi
if [[ ${newstr} -ge $2 ]]
then
echo '============================'
echo ''
echo 'CRITICAL: TEMPERATURE CORE' $i 'EXCEEDED' $2 '=>' $newstr
echo ''
echo '============================'
sudo pm-suspend
echo 'Sleeping....'
#exit
else
echo ' Temperature Core '$i' OK at =>' $newstr
echo ''
fi
done
done
echo 'Both CPU Cores are within limits'
echo ''