バッテリーが特定の低いしきい値に達したときにラップトップをスリープ状態にするにはどうすればよいですか?

バッテリーが特定の低いしきい値に達したときにラップトップをスリープ状態にするにはどうすればよいですか?

私はUbuntuを使用していますが、デスクトップ環境ではなくウィンドウマネージャとしてi3を使用しています。

バッテリーが0%に達すると、警告やその他の情報なしでコンピュータが突然シャットダウンします。

バッテリーが4%のときにスリープモードに切り替えるように設定できる簡単なスクリプトまたは設定はありますか?

ベストアンサー1

これは、バッテリーレベルを確認し、pm-hibernateバッテリーレベルが特定のしきい値を下回った場合にカスタムコマンドを呼び出す小さなスクリプトです。

#!/bin/sh

###########################################################################
#
# Usage: system-low-battery
#
# Checks if the battery level is low. If “low_threshold” is exceeded
# a system notification is displayed, if “critical_threshold” is exceeded
# a popup window is displayed as well. If “OK” is pressed, the system
# shuts down after “timeout” seconds. If “Cancel” is pressed the script
# does nothing.
#
# This script is supposed to be called from a cron job.
#
###########################################################################

# This is required because the script is invoked by cron. Dbus information
# is stored in a file by the following script when a user logs in. Connect
# it to your autostart mechanism of choice.
#
# #!/bin/sh
# touch $HOME/.dbus/Xdbus
# chmod 600 $HOME/.dbus/Xdbus
# env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
# echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus
# exit 0
#
if [ -r ~/.dbus/Xdbus ]; then
  source ~/.dbus/Xdbus
fi

low_threshold=10
critical_threshold=4
timeout=59
shutdown_cmd='/usr/sbin/pm-hibernate'

level=$(cat /sys/devices/platform/smapi/BAT0/remaining_percent)
state=$(cat /sys/devices/platform/smapi/BAT0/state)

if [ x"$state" != x'discharging' ]; then
  exit 0
fi

do_shutdown() {
  sleep $timeout && kill $zenity_pid 2>/dev/null

  if [ x"$state" != x'discharging' ]; then
    exit 0
  else
    $shutdown_cmd
  fi
}

if [ "$level" -gt $critical_threshold ] && [ "$level" -lt $low_threshold ]; then
  notify-send "Battery level is low: $level%"
fi

if [ "$level" -lt $critical_threshold ]; then

  notify-send -u critical -t 20000 "Battery level is low: $level%" \
    'The system is going to shut down in 1 minute.'

  DISPLAY=:0 zenity --question --ok-label 'OK' --cancel-label 'Cancel' \
    --text "Battery level is low: $level%.\n\n The system is going to shut down in 1 minute." &
  zenity_pid=$!

  do_shutdown &
  shutdown_pid=$!

  trap 'kill $shutdown_pid' 1

  if ! wait $zenity_pid; then
    kill $shutdown_pid 2>/dev/null
  fi

fi

exit 0

これは非常に簡単なスクリプトですが、アイデアを理解し、必要に応じて簡単に適用できると思います。バッテリーの電源経路はシステムによって異なる場合があります。より移植性の高いアプローチは、同様の方法を使用してacpi | cut -f2 -d,バッテリ電源を得ることであり得る。このスクリプトは、cronを介して1分ごとに実行するようにスケジュールできます。 crontabを編集しcrontab -eてスクリプトを追加します。

*/1 * * * * /home/me/usr/bin/low-battery-shutdown

別の解決策は、GnomeやXfceなどのデスクトップ環境をインストールし、ウィンドウマネージャをi3に変更することです。上記の両方のデスクトップ環境には、コンピュータの電源を切るための電源管理デーモンがあります。しかし、私はあなたが意図的にそれらを使用せずにより最小限の解決策を探していると仮定します。

おすすめ記事