Ubuntu: To control battery at a certain capacity (for ex.20%) then the laptop will be automatically standby or hibernate for long-lasting the battery



Question:

I would like to control for setting my battery at a certain capacity (for ex.20%) then the laptop will be automatically standby or hibernate for long-lasting the battery. Is there any solution, please? Be noted: my Vostrol 3450, ubuntu 13.04, hibernate function is already rehabilitated.

The battery capacity I can find is in the file capacity as below:

/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0A:00/power_supply/BAT0/capacity  

I created the bash script:

#!/bin/bash    load=$(cat /sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0A:00/power_supply/BAT0/capacity)  min_load=20  if [ $load <= min_load ]; then      pm-suspend  fi  

On terminal, I perform the cli for test:

sudo /bin/battery-suspend  

Then the result failed showing:

/bin/battery-suspend: line 5: =: No such file or directory  


Solution:1

The battery capacity is stored somewhere in /proc. Possibly /proc/acpi/battery or something like that. You could write a bash script that reads the battery status and issues a hibernate or suspend command. It would look something like this:

#!/bin/bash    load=$(cat /proc/acpi/battery)  min_load=20  if [ $load <= min_load ]; then      pm-suspend  fi  

I give no guarantee that this is right since I don't know how the output of cat /proc/acpi/battery looks like and I'm not that big of an bash script expert. Try it out and ask if you have any problems.

You could then create a cronjob that runs the script every minute or so. Take a look at this tutorial for cronjobs. You want to create your job as root user.


Solution:2

You can try to use this script.

It raise a notification and then suspend your PC when battery level reach selected value.

#! /bin/bash    # read battery percentage value  OUT=`upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep percentage`    # select only the int value  IFS=':' read -ra P <<< "$OUT"  PERCENTAGE="%"  BATTERY_VALUE=${P[1]%$PERCENTAGE}    # send a notification and then suspend pc  # if battery level is under 20%  if (( $BATTERY_VALUE  <= "20")); then    notify-send "Battery Low level! Your PC will be suspended!"      # play sound if you want to be alerted    # paplay /usr/share/sounds/freedesktop/stereo/complete.oga      # wait for 3 seconds before suspend pc    sleep 3    # suspend your pc    pm-suspend  fi  

Copy this script into a bash file and move it into local bin folder:

sudo mv <script_file> /usr/local/bin/  

Where script_file is the name (or path) of your script. Then you can use cron daemon to run it every 5 minutes (or select your execution frequency) to check battery level. So edit cron as superuser (script need super user permission to suspend PC):

sudo crontab -e  

Select preferred editor and add at the end of file this line:

*/5 * * * * /usr/local/bin/<script_file>  

Change script_file with your script name.

Now it should work. Check it after a system reboot.

I hope this will help you.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »