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
1 Answer
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.