Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CPU警告bash脚本_Bash_Shell - Fatal编程技术网

CPU警告bash脚本

CPU警告bash脚本,bash,shell,Bash,Shell,下面是我尝试制作一个将休眠的脚本: 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 s

下面是我尝试制作一个将休眠的脚本:

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。它是空的。脚本的基本思想是,当温度达到一定水平时,让电脑“休眠”

$i
未定义,从显示的代码开始

如果
$i
为空,
$str
将为空
$newstr
是$str的子字符串,索引错误

因此,
$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语法有点…嗯…

这对我很有用:

#!/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 ''

核心$i
中,您在哪里设置
$i
?在设置newstr之前测试具有值的str(或在将其设置为substr之后测试newstr)。它失败了,因为grep没有找到匹配的$i,因此str是空的。
bash-x
要进行故障排除,我设法修复了这个脚本,现在我的电脑将在硬件进入最高温度并切断电源之前休眠。:-)好的答案没有帮助吗?这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论。您是舒尔吗?你做过测试吗?很简单,你最初的答案不是一个真正的答案,而是一个评论。如果你想详细说明,可以认为这是一个完整的答案。