Monday, September 6, 2010

[complete]sysmon-0.0.2-2

Finished it all. I accidentally left an echo in the previous release. So, here's the completed script.

#!/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`"

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


And here's the init script that I wrote up for it.
#!/bin/sh

. /etc/rc.d/functions

PID="`pidof -x sysmond 2>/dev/null`"

case "$1" in
start)
stat_busy "Starting System Monitor"
/usr/bin/sysmond &
if [[ $? -gt 0 ]]; then
stat_fail
else
stat_done
fi
;;
stop)
stat_busy "Stopping System Monitor"
[[ ! -z $PID ]] && kill $PID
if [[ "$?" = "0" ]]; then
stat_done
else
stat_fail
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "ussage: $0 {start|stop|restart}"
esac
exit 0

No comments:

Post a Comment