Posts: 347
Silent Observer
Joined: 08 Aug 2013
#1
Okay, now that
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"antix.freeforu ms.org/post35719.html"
linktext was:"I have conky correctly reporting my battery state"
====================================
, I need to find a utility that can run as a daemon, monitor my battery condition, and shut down my laptop (Pentium II, ca. 1997, antiX 13.2 testing kept updated, kernel currently 3.13-0.bpo.1-486, fluxbox) at a preselected level of remaining charge -- so as to get an orderly shutdown before loss of power causes system corruption, as it has done in the past. Given there are literally tens of thousands of packages in the Debian testing repos, and I have a number of useful programs that aren't available in the repos at all and I didn't even know to look for until someone mentioned them, it's a lot more sensible to just ask...
Posts: 4,164
rokytnji
Joined: 20 Feb 2009
#2
Just something you can look at.

batti-gtk-solved-t4662.html
Posts: 347
Silent Observer
Joined: 08 Aug 2013
#3
Batti looks like a potential solution; I installed gnome-power-manager from the repos, but all I find in the menu after installing it is"power statistics", with no indication of the other event-driven capabilities shown in the package description (suspend, hibernate, shutdown, plus display of notifications for all those events); I wonder if those don't require I be running actual gnome desktop or if there's more setup needed to start the daemon (or if I just need to restart antiX).

Just a monitor doesn't do the job here; I've got that now with conky (though it reports the battery at 100% when it's pulled out of its socket, I only dismount it when it needs to cool after a charge cycle overheat). Problem is, when I leave this machine running, it's in screen saver with a sound file playing, so I need the software to *do something* without user interaction when power gets low. Complicating that, I've had poor/no results with suspend and hibernate on this machine (I'm not sure it can genuinely do either); it's got a standby mode, but that seems to be just reducing CPU clock to zero and if left that way too long, will"wake" with a hard lockup. I'd rather have a hard shutdown and risk loss of data from an open app (a very uncommon condition for this machine) than risk a power loss because no one can see an on-screen warning.
Posts: 347
Silent Observer
Joined: 08 Aug 2013
#4
Oh, sweet! Way, way back in the Conky Configuration thread, I found some clues (thanks, Dave!) and that led me to
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://crunchbang.org/forums/viewtopic.php?pid=46185"
linktext was:"this thread on the Chrunchbang forum"
====================================
with conky commands that start a script on low or very low power conditions. I think I can modify those to check for AC adapter condition (online vs. offline), and give a warning popup on low battery, with an automatic shutdown at the very low condition. It'll take a bit more research, but maybe I can print off the conky man page (or load it into my book reader) and work on increasing my level of understanding...
Posts: 1,062
Dave
Joined: 20 Jan 2010
#5
post17676.html?hilit= battery warning conky#p17676

Not sure if this is the one you found, but I know I have done it before. Unfortunately I don't think that I have the scripts anymore but they should be a lot easier to make with yad.
Posts: 347
Silent Observer
Joined: 08 Aug 2013
#6
I found one on the crunchbang thread that not only already checks for online vs. offline ac adapter, but has python scripts (included!) to handle notification and, at need, shutdown. Time to get rid of gnome-power-manager -- too fat! -- and get working on making conky do this job.
Posts: 347
Silent Observer
Joined: 08 Aug 2013
#7
Okay, here's a question...

From the thread I linked above, I lifted the following (slightly modified) ~/.conkyrc fragment:

Code: Select all

${color}Power: ${color}$acpiacadapter, ${color}Battery: ${battery_percent CMB1}% 
${if_existing /sys/class/power_supply/AC/online 0}
    ${if_match ${battery_percent CMB1} <= 10}
        ${if_match ${battery_percent CMB1} <= 5}
            ${if_match ${battery_percent CMB1} <= 3}
                ${color red}                
                ${exec shutdown -ahP -t 30 now"Shutting down 30 seconds; critical low battery!"}
            $else
                ${color red}
                ${execi 60 /usr/local/bin/criticalbat.py}
            $endif
        $else
            ${color orange}
            ${execi 120 /usr/local/bin/lowbat.py}
        $endif
    $endif
$endif${battery_bar 4,180 BAT1}
combined with these two python scripts, lowbat.py

Code: Select all

$ cat /usr/local/bin/lowbat.py
#!/usr/bin/env python
import pynotify
pynotify.init("foo")
pynotify.Notification("Power warning","Battery capacity is low.","battery-low").show()
and criticalbat.py

Code: Select all

$ cat /usr/local/bin/criticalbat.py
#!/usr/bin/env python
import pynotify
pynotify.init("foo")
pynotify.Notification("POWER WARNING!","Battery capacity is critically low!","battery-caution").show()
I'm preferring shutdown because I've never managed to get this laptop to either hibernate (at all) or suspend (for more than a second or two, before spontaneously reviving), neither of which will save me in case of an emergency low power shutdown. Unfortunately, shutdown is an admin privilege command (i.e. bash claims it doesn't exist if it's invoked by an ordinary user). I presume that if conky is running with root privilege, it'll be able to launch shutdown when needed; how do I give conky root privilege, automatically with every login?
Posts: 850
fatmac
Joined: 26 Jul 2012
#8
Maybe in etc/sudoers, allow yourself to use the shutdown or poweroff command (?).
Posts: 347
Silent Observer
Joined: 08 Aug 2013
#9
The problem with sudo is it asks for a password, and there doesn't seem to be a way to let an ordinary user issue an admin-privilege command without using sudo.

I found a work-around in the sudo man page; apparently, it's possible to get sudo to pull the password from stdin and drop the prompt into stderr, both of which can be redirected to files, instead of reading/writing on console. This requires leaving my user password in plain text in a file on the hard disk, but someone would have to have that password to get into the computer in the first place (screen saver password locks, as well as needing a login on startup), so it's not a big risk (and the file to look in is completely arbitrary -- can be hidden, anywhere in the file system, etc.). I haven't tested with actual low battery yet (maybe today I'll take the computer to the other room and play for a while), but I've verified that the shutdown command I need works this way.
Posts: 1,062
Dave
Joined: 20 Jan 2010
#10
There is a way to make it so that you do not need to have a password for a sudo command. I thought shutdown was already set up like that but I guess not.

Iirc it is formated as
users all(root) nopasswd:all / bin / shutdown

I'm not sure if this the exact line but it should be close. The key is the nopasswd option.
Posts: 347
Silent Observer
Joined: 08 Aug 2013
#11
I'll dig in the sudo and sudoers man pages some more and look for that option -- though I don't know as that's any more secure than leaving my password in a file on a machine with only one user account (mine). I tested all the way down to 3% battery, however, and found that neither the python notification scripts nor the shutdown invocation worked. As far as I can tell, conky's decision process is working correctly (battery bar changes color as it should), but the python scripts don't work when called from a terminal (I'll post the error outputs; very unhelpful messages!) and either sudo or the shutdown command fail when invoked by conky -- I'm going to test with sudo and shutdown running from a script to see if it's that works better for conky's exec object.

Python errors:

Code: Select all

$ /usr/local/bin/lowbat.py
Traceback (most recent call last):
  File"/usr/local/bin/lowbat.py", line 4, in <module>
    pynotify.Notification("Power warning","Battery capacity is low.","battery-low").show()
glib.GError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Notifications was not provided by any .service files
dqualls@Little-Luddite:~
$ /usr/local/bin/criticalbat.py
Traceback (most recent call last):
  File"/usr/local/bin/criticalbat.py", line 4, in <module>
    pynotify.Notification("POWER WARNING!","Battery capacity is critically low!","battery-caution").show()
glib.GError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Notifications was not provided by any .service files
Posts: 1,062
Dave
Joined: 20 Jan 2010
#12
Try this as a critical shutdown script, instead of the python one.

Code: Select all

#!/bin/bash
yad --image="info" --text="the battery is critically low. Save your work and press OK to shutdown" --button="gtk-ok:0" --button="gtk-cancel:1"
ans="$?"
case $ans in
0)
    # shutdown 
    sudo shutdown -h now 
;;
1)
    #exit
    exit;
;;
esac

Dave
Posts: 347
Silent Observer
Joined: 08 Aug 2013
#13
The script I'm trying for shutdown is just a one-line bash script:

Code: Select all

#!/bin/bash
sudo -S < passwordfile &> dumpfile shutdown -hP 1"Shutting down in one minute; critical low battery!"
I know this command works at the command line (I'm less sure I'll see it if I don't have a terminal open), but it's not getting launched from conky. The python scripts are to notify the user (me!) before shutdown is under way, but they're failing with an error message when I launch them from command line.

Edit: Well, the file above works, but the one I had stored didn't -- bad file paths for password file and dump file (due to moving the file after creation and initial testing), and unneeded -a option. Working now, and invokes correctly from conky. Now, if I can get the pytho notification to work, I'll be good to go. Could a variant of the yad script above do the same job?
Posts: 1,062
Dave
Joined: 20 Jan 2010
#14
Silent Observer wrote: Could a variant of the yad script above do the same job?
Yes, the one above should be able to present you a dialog showing your battery is critically low but give you time to save before okaying the shutdown. However you can change the command that executes the shutdown to whatever you want. I have not tested the script myself though, just typed it out on the phone...


You need to keep in mind that every time conky refreshes it will reload this window... so either make your execi time long or we need to do some bash sorcery to make sure that no other warning windows are open. __{{emoticon}}__
Posts: 1,308
BitJam
Joined: 31 Aug 2009
#15
The commands /sbin/halt and /sbin/reboot are already in /etc/sudoers.d/antixers.

In antiX-14 /usr/local/bin/halt and /usr/local/bin/reboot will be added as well. Use"/sbin/halt" to shutdown the system and"/sbin/reboot" to reboot.

On a Live system with persistence then you will want to use the /usr/local/bin/ versions (in antiX-14) or you will have to take care of the running of persist-save when you shutdown. Note that if the user has selected semi-automatic persistence saving then the persist-save program will run interactively.