0

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

2 Answers 2

0

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.

3
  • I tried to perform, but it's failed: "cat: /proc/acpi/battery: No such file or directory", What can I do for next?
    – evergreen
    May 13, 2013 at 10:08
  • Like I said it's only possibly /proc/acpi/battery. Take a look into your /proc directory and try to find the file containing your battery status. There is really no default solution for this. You could use acpi -V (possibly install it first with sudo apt-get install acpi) but then you'd have to parse the output for the capacity. May 13, 2013 at 11:05
  • What I can find described as above, hope it the right one. Could you help for creating a right bash script?
    – evergreen
    May 13, 2013 at 17:13
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .