Is there a way to shutdown a linux-based computer when the battery charge drops below a certain level? I have searched through the shutdown and poweroff manuals but found nothing relevant.
-
1There must be a way, because that's what the Battery Monitor does on my Linux laptop.– chorobaCommented Mar 27, 2016 at 13:24
-
1I believe this is commonly done already in many distributions. Which one are you using?– l0b0Commented Mar 27, 2016 at 13:26
-
@atsag Knowing your distro and desktop environment would be very useful here.– acejavelinCommented Mar 27, 2016 at 14:51
-
Thank you everyone for taking the time! I am using Linux Mint 17.3, but if there would be a more general solution - one covering more unices - that would be preferable.– atsagCommented Mar 27, 2016 at 18:42
-
Google is your friend - unix.stackexchange.com/questions/84437/… and bbs.archlinux.org/viewtopic.php?id=162900– davidgoCommented Mar 28, 2016 at 2:41
2 Answers
All right! I did some more thorough research on the topic, and it appears that this can be done with a script. The source of inspiration for my script below, is the following topic in the arch linux forums:
https://bbs.archlinux.org/viewtopic.php?id=162900 ,and more specifically the first post by vacant.
#!/bin/bash
BATTINFO=`acpi -b`
CRITICAL_PERCENTAGE=21
FILE_LOCATION="/home/username/LOW_BATTERY" #change this to correct username!
if [[ `echo $BATTINFO | grep Discharging` && `echo $BATTINFO | cut -c 25-26 ` -lt $CRITICAL_PERCENTAGE ]]
then
echo `date` >> $FILE_LOCATION
echo "Was forced to hibernate, due to low battery status">>$FILE_LOCATION
echo $BATTINFO >> $FILE_LOCATION
sudo pm-hibernate
fi
The job can be automated using a cron job, by editing the root user's crontab file (e.g using su in a terminal, and then crontab -e), for the script to be executed once in every regular interval (e.g 2 minutes). As mentioned by davidgo above, there is a (very) similar question answered in http://unix.stackexchange.com, that could be used as a reference for a more advanced approaching.
Here you can find useful UDEV rules for this: https://wiki.archlinux.org/title/Laptop#Hibernate_on_low_battery_level
just replace "hibernate" with "poweroff" in the suggested UDEV rule file content and it should switch off your computer instead of hibernating it.
Here is the code snippet you should save in /etc/udev/rules.d/99-lowbat.rules:
# Suspend the system when battery level drops to 5% or lower
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[0-5]", RUN+="/usr/bin/systemctl poweroff"
Before doing that, make sure your battery is sending UDEV messages as the discharge proceeds, otherwise the aforementioned code will not work. You can watch for new UDEV messages with:
udevadm monitor --property
If this does not show any message while the battery discharges, you have to use another method like adding this script to cron, also taken from the same link above (but it requires you to install acpiclient, that seems is not packaged for Debian):
#!/bin/sh
acpi -b | awk -F'[,:%]' '{print $2, $3}' | {
read -r status capacity
if [ "$status" = Discharging -a "$capacity" -lt 5 ]; then
logger "Critical battery threshold"
systemctl poweroff
fi
}
In case you do not have the acpi command available, you can use this small script (please let me know it it does not work for you. In case there is more than one battery, it will shutdown the computer if any of these is discharging below 10%):
#!/bin/sh
for battery in /sys/class/power_supply/*bat*
do
status=`cat $battery/status`
capacity=`cat $battery/capacity`
if [ "$status" = Discharging -a "$capacity" -lt 10 ]; then
logger "Critical battery threshold"
systemctl poweroff
fi
done
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review– TotoCommented Jul 31, 2024 at 14:59