I want to plug in the laptop battery charger when the battery level goes down to 40% and then plug it out when the battery charge level reaches 80%. And for that reason, I need a script that would remind me to plug in the charger when the battery level is 40% and again would remind me when the battery charge level reaches 80%. How would be the script? Could anything else do that?
Shell – Script for “battery level reminder”
batterylaptopshell-script
Related Solutions
Here's a small script that checks for the battery level and calls a custom command, here pm-hibernate
, in case the battery level is below a certain threshold.
#!/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
. ~/.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
It's a very simple script, but I think you get the idea and can easily adapt it to your needs. The path to the battery level might be different on your system. A little more portable would probably be to use something like acpi | cut -f2 -d,
to obtain the battery level. This script can be scheduled by cron to run every minute. Edit your crontab with crontab -e
and add the script:
*/1 * * * * /home/me/usr/bin/low-battery-shutdown
Another solution would be to install a desktop environment like Gnome or Xfce (and change your window manager to i3). Both mentioned destop environments feature power management daemons which take care of powering off the computer. But I assume you deliberately don't use them and are seeking for a more minimalistic solution.
Suspending implies that the data you were working with is in the memory. This state will be lost if you run out of power. If you were writing a forum post, for example, it will be lost, but nothing more serious would happen. Think that it's less serious than unplugging and removing the battery, since the computer can sync before and even delay the suspension (when running an update or upgrade).
For obtaining both the benefits of suspend (fast re-start) and hibernate (safely saved to disk, without power consumption) try pm-suspend-hybrid
instead. If you run out of power you can re-start from the image saved to disk, if you don't run out of power you can quickly re-start from a suspended to memory state.
Best Answer
Try this one. In my ubuntu 12.04 it is working perfectly.