Monday, September 6, 2010

sysmon0.0.1-4 -- Monitor cpu temp and battery charge for unacceptable levels

I wrote this script with intent to background. If the battery charge gets below 5% or the cpu temperature climbs above 80C, the command to halt is issued.

#!/bin/zsh

# define battery variables
charge_now='/sys/class/power_supply/BAT1/charge_now'
charge_full='/sys/class/power_supply/BAT1/charge_full'
charge_factor=20 # 5% battery power

# define temperature variables
max_temp=80
for i in {0..1}; do
temp_path="/sys/class/hwmon/hwmon$i/device/temp1_input"
[[ -f $temp_path ]] && break
done

# get full charge value
full=`cat $charge_full`

# just keep going
while [[ "1" = "1" ]]; do
# get current charge
charge=`cat $charge_now`

# calculate the minimum charge based on the charge_factor
charge_min=$(($full/$charge_factor))

# system doesn't use hwmon1 or hwmon0 exclusively, so there's a need to differentiate
temp="`cat $temp_path`"
[[ "$?" = "0" ]] || continue

[[ $temp > 80 ]] || [[ $charge < $min_charge ]] && `shutdown -h now` sleep 10 done


This is a running version. I have not tested to see if it will execute properly once the conditions are met. Setting it to short term list, most likely tomorrow.

No comments:

Post a Comment