2020年08月09日 Setting ACPI for suspending machine automatically when battery charge loss [長年日記]
_ ノートPCのバッテリー残量が少なくなったら自動的にサスペンドするためのACPI設定方法
/etc/acpi/acpi_handler.sh
バッテリーに関するイベントを検知したらスクリプト(/etc/acpi/lowbattery.sh)を起動する。
*** acpi_handler.sh.ORG 2020-08-08 23:40:56.530432033 +0900
--- acpi_handler.sh 2020-08-09 01:16:38.793951977 +0900
***************
*** 13,18 ****
--- 13,21 ----
;;
esac
;;
+ battery)
+ /etc/acpi/lowbattery.sh &
+ ;;
*)
logger "ACPI group $1 / action $2 is not defined"
;;
/etc/acpi/lowbattery.sh
バッテリー駆動の間は60秒間隔でバッテリー残量をチェックし、残量が10%を切った場合サスペンドする。ACアダプタ駆動になったら終了する。
#! /bin/sh
trap 'rm -fr /tmp/lowbattery.lock' INT TERM
mkdir /tmp/lowbattery.lock || exit 1
wait=60
threshold_percent=10
while [ X = X ]
do
if [ X`cat /proc/acpi/ac_adapter/ACAD/state | egrep off-line` = "X" ]
then
off_line=no
else
off_line=yes
fi
if [ ${off_line} = yes ]
then
last_full_capacity=`cat /proc/acpi/battery/BAT1/info |\
egrep 'last full capacity' |\
sed -e 's/: */:/;s/ mAh//' |\
cut -d: -f2`
remaining_capacity=`cat /proc/acpi/battery/BAT1/state |\
egrep 'remaining capacity' |\
sed -e 's/: */:/;s/ mAh//' |\
cut -d: -f2`
battery_percent=`expr ${remaining_capacity} \* 100 / ${last_full_capacity}`
if [ ${battery_percent} -le ${threshold_percent} ]
then
logger "ACPI /etc/acpi/lowbattery.sh suspend(${battery_percent}%/${threshold_percent}%)"
sync;sync;sync
echo -n mem > /sys/power/state
fi
else
break
fi
sleep ${wait}
done
rm -fr /tmp/lowbattery.lock
[ツッコミを入れる]