[solved] cli-power-manager (autoshutdown)

Forum rules
We don't support installations in VirtualBox, VMWare, qemu or others. We ignore posts about WINE, PlayOnLinux, Steam and Skype. We don't support btrfs, lvm, UEFI, side-by-side installations with GPT or dualboot with anything newer than Windows XP.
Google your problem first. Check the Wiki. Read the existing threads. It's okay to "hijack" an existing thread, yes! If your problem is not yet covered, open a new thread. To get the quickest possible help, mention the exact release codename in your post (uname -a is a good idea, too). Due to the lack of crystal balls, attach the output of lspci -nnk if you encounter hardware problems.
mamaw
Riesenpenis
Posts: 33
Joined: Thu Sep 26, 2013 3:30 am

[solved] cli-power-manager (autoshutdown)

Unread post by mamaw » Tue Oct 08, 2013 4:06 am

hello there script-gurus

once in a while, i left my laptop on unplugged till the battery drop. (fall asleep or stuff)
when comes to GUI, there's some choices of using power manager to auto shutdown or hibernate when the battery has reach some amount of level predefined.

but what about cli?
to monitoring the battery level maybe i can run
$ watch --interval=xx acpi

is there any how-to's to auto shutdown when battery is critical, say 5% in cli environtment?

thanks in advance
Last edited by mamaw on Fri Oct 11, 2013 5:05 am, edited 1 time in total.

it all begin from curiousity

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: cli-power-manager

Unread post by machinebacon » Tue Oct 08, 2013 4:15 am

Hello,
yes, that should be possible. Just quickly confirming,

Code: Select all

cat /sys/class/power_supply/BAT0/energy_now
is the value we need, now the question is only: how much is 5%
We have a value 'full' coming from

Code: Select all

cat /sys/class/power_supply/BAT0/energy_full_design
So, we could write a start service (in /etc/rc5.d/) that is executed every 600 seconds (10 minutes), which reads the energy_now value, and as soon as it falls below a level, a xmessage pops up to save all documents and then, again after 90 seconds, to shutdown.

Just an idea.
..gnutella..

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: cli-power-manager

Unread post by machinebacon » Tue Oct 08, 2013 4:18 am

Who is our friend? Google. "monitor battery debian no-X" brings http://blog.sleeplessbeastie.eu/2013/01 ... -capacity/
There are quite some ideas, just the paths have to be changed a bit. See the "RRDtool" part, where he uses 'watch' to read the output of a script. The script would only grep/echo the energy_now value, which needs to be played with.
..gnutella..

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: cli-power-manager

Unread post by machinebacon » Tue Oct 08, 2013 4:25 am

Here's a solution using conky: http://linuxmintdebian.blogspot.com/200 ... rning.html

Instead of conky, make a watch line in /etc/rc.local that calls the 'warn-shutdown' script (which is executable and with the right permissions)
..gnutella..

mamaw
Riesenpenis
Posts: 33
Joined: Thu Sep 26, 2013 3:30 am

Re: cli-power-manager

Unread post by mamaw » Tue Oct 08, 2013 4:34 am

thanks MB, i will tinker with it a bit.
i did ask uncle google several times, but i guess i miss the right keyword :(

it all begin from curiousity

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: cli-power-manager

Unread post by machinebacon » Tue Oct 08, 2013 4:45 am

People will anally rape me for saying this, but I use a certain email address to log in to Google Services, and it seems to me that after a while the search engine learns which kind of posts should be displayed first. After having used this service for several months, I must say that it has convinced me, even though the privacy-sensitive readers already prepare the vaseline for me.
..gnutella..

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: cli-power-manager

Unread post by rhowaldt » Tue Oct 08, 2013 10:00 am

privacy is bloat.
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: cli-power-manager

Unread post by dkeg » Tue Oct 08, 2013 12:55 pm

i've used conky solution for coloring the batt percentage when low. Still miss it sometimes though.

google is very dissappointed in my offerings.

Work hard; Complain less

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: cli-power-manager

Unread post by machinebacon » Tue Oct 08, 2013 1:01 pm

One could send the warning to dunst (or any other notifier daemon), or send a beep, play a sound file, open an xmessage, or make the keyboard indicators blink.
..gnutella..

User avatar
ivanovnegro
Minister of Truth
Posts: 5449
Joined: Wed Oct 17, 2012 11:12 pm

Re: cli-power-manager

Unread post by ivanovnegro » Tue Oct 08, 2013 11:01 pm

machinebacon wrote:I must say that it has convinced me, even though the privacy-sensitive readers already prepare the vaseline for me.
:D It is true Jules. It works like spam filters.

mamaw
Riesenpenis
Posts: 33
Joined: Thu Sep 26, 2013 3:30 am

Re: cli-power-manager

Unread post by mamaw » Fri Oct 11, 2013 5:04 am

so, finally ... (how many days already?)

with my zero knowledge of bash :D #laugh
i managed to write this first bash script of mine:

Code: Select all

#!/bin/bash

while true
do

bat1=500000 #or any (critical) battery value
bat2=`cat /sys/class/power_supply/BAT1/energy_now`
sleep 5s 

if [ "$bat1" -gt "$bat2"] ; then
sudo shutdown -h now
fi
done


this required passw, so i add to visudo

%sudo ALL=NOPASWWD: /sbin/shutdown

and this solved it. cheers..
----------------------------------------
i believe the script is far from "proper" ;)

it all begin from curiousity

mamaw
Riesenpenis
Posts: 33
Joined: Thu Sep 26, 2013 3:30 am

Re: [solved] cli-power-manager (autoshutdown)

Unread post by mamaw » Wed Oct 23, 2013 7:10 pm

a little update, to show the percentage of the battery from tmux :p

Code: Select all

#!/bin/bash

red='\e[0;31m'
yellow='\e[1;33m'
NC='\e[0m'
bat1=3500000
bat3=`cat /sys/class/power_supply/BAT1/energy_full_design`

while true
do
clear
bat2=`cat /sys/class/power_supply/BAT1/energy_now`
bat4=`echo "scale=2;($bat2*100)/$bat3" | bc`
stat=`cat /sys/class/power_supply/BAT1/status`
echo $bat4"%" $stat
sleep 5s
if [ "$bat1" -gt "$bat2" ] ; then
	clear
	echo -e $bat4"% ${yellow}Critical Battery${NC}" 
	echo -e "Press ${red}'ctrl+c'${NC} to skip shutdown. Shutdown in 60 second..."
	sleep 60s
	sudo shutdown -h now
	exit 1
fi
done

the output:
44.03% Discharging

*i login to openbox for a while since i miss those colorfull pic instead of:

Google
[___________________________________] Advanced search Language tools
[Google Search] [I'm Feeling Lucky]

it all begin from curiousity

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: [solved] cli-power-manager (autoshutdown)

Unread post by machinebacon » Wed Oct 23, 2013 7:53 pm

^sweet ;)

You can try, in TTY without tmux:
xlinks2 google.com

There you have your colorful pic :)
..gnutella..

Post Reply