# Battery Power



## yhq_34 (Jan 9, 2016)

Hi ALL;

How can I setup, like my laptop is using battery, if the power was lower than 20%, and the computer will shutdown?


----------



## aragats (Jan 12, 2016)

You can run a shell script either via cron or just adding a loop with sleep in the script itself, e.g.:

```
#!/usr/local/bin/bash

CRITICAL=20
STATCMD='acpiconf -i 0'
LOOPDEL=30

while true
do
  REMCAP=`$STATCMD | sed -n 's/Remaining\ capacity:[^0-9]*\([0-9]*\)[^0-9]*/\1/p'`
  if [ $REMCAP -le $CRITICAL ]
  then
    <halt or suspend command>
  fi
  sleep $LOOPDEL
done
```


----------



## Juha Nurmela (Jan 12, 2016)

```
set -- `sysctl -n hw.acpi.acline hw.acpi.battery.life`
```

This would get remaining capacity (or empty string) to $2. What happens when there are two batteries ? I've always  have just one.

Juha


----------



## tobik@ (Jan 12, 2016)

Shouldn't devd(8) receive an ACPI event for this?


----------



## ANOKNUSA (Jan 12, 2016)

If I were you, I'd run a cron job that spat out a desktop notification (via `notify-send`) reminding me to check my battery life every ten minutes. I refuse to believe anyone actually appreciates having their computer shut off when they're in the middle of doing something.


----------



## RedShift1 (Jan 13, 2016)

ANOKNUSA said:


> If I were you, I'd run a cron job that spat out a desktop notification (via `notify-send`) reminding me to check my battery life every ten minutes. I refuse to believe anyone actually appreciates having their computer shut off when they're in the middle of doing something.


You could suspend instead of shutting it down


----------



## yhq_34 (Jan 13, 2016)

aragats said:


> You can run a shell script either via cron or just adding a loop with sleep in the script itself, e.g.:
> 
> ```
> #!/usr/local/bin/bash
> ...


How can I setup a cron job to use your script?


----------



## ANOKNUSA (Jan 13, 2016)

RedShift1 said:


> You could suspend instead of shutting it down



The machine still "shuts off." Your work will be interrupted--no matter which command you run, your mindless machine gets to decide when you're done working. Get into the habit of actually checking your battery life, so you're not cut off in the middle of something important.

I guess my real point is that people who find this "feature" useful probably are just using their computers to kill time anyway, since they don't seem to actually care how much of it they have left. If you cared, you wouldn't be turning off your brain and letting the machine tell you when you're done.



yhq_34 said:


> How can I setup a cron job to use your script?



Read the relevant section of the _FreeBSD Handbook_ and Google "cron job." Cron has been around for over forty years--information's not scant.


----------



## yhq_34 (Jan 13, 2016)

I want add a condition: it's only battery is discharging, it will shutdown.


----------



## Crivens (Jan 13, 2016)

You may find sysutils/battmond helpful.


----------



## Carpetsmoker (Jan 22, 2016)

sysutils/battray is also a nice tool (disclaimer: I am the author, so of course I'm going to say that ;-)).


----------



## Kiiski (Jan 24, 2016)

Maybe you would be interested of doing like I did for my laptop.

Laptop checks during boot if there is no acline available.
If so, it starts script that monitors battery capacity.
Script also monitors acline and exits if it comes on.
When capacity drops below threshold value, it gives loud warning to either turn acline on or it will shutdown the machine after safety period.

If acline is on during boot, script will not be started by boot process. However, devd(8) starts the script if acline is lost.


----------



## scottro (Jan 24, 2016)

Carpetsmoker said:


> sysutils/battray is also a nice tool (disclaimer: I am the author, so of course I'm going to say that ;-)).



I wasn't aware of that.  Pretty useful, thanks.


----------

