You are not logged in.

#1 2014-01-19 21:41:05

Arkranur
Member
Registered: 2013-07-27
Posts: 23

How can I make an udev rule for a low battery pop-up warning message?

Following this section of the wiki, I created my own udev rule in etc/udev/rules.d/criticalbat.rules containing

# Pop up warning message when battery is at 10% and discharging
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="10", RUN+="/usr/bin/xmessage BatteryAt10%"

But after I ran as root

 udevadm control --reload-rules 

the xmessage window would not pop-up at all.

Offline

#2 2014-01-19 22:22:03

rodyaj
Member
Registered: 2009-10-13
Posts: 54

Re: How can I make an udev rule for a low battery pop-up warning message?

Not all computers send events internally when the battery percentage changes, but maybe try adding env DISPLAY=:0 before /usr/bin/xmessage.

I use a cron job to run a script periodically. For example, install libnotify and then make a script /home/youruser/.bin/battery-check:

#!/bin/sh
export XAUTHORITY=/home/joe/.Xauthority
export DISPLAY=":0"

battery_level=`acpi -b | cut -d ' ' -f 4 | grep -o '[0-9]*'`
if [ $battery_level -le 10 ];
then
    notify-send "Battery low" "Battery level is ${battery_level}%!"
fi

if [ $battery_level -le 3 ];
then
    # your custom actions here e.g.,                                                                  
    # systemctl hibernate
fi

You may need to chmod +x the above script to make it executable (not sure).

Now edit the user crontab with crontab -e:

*/5 * * * * env DISPLAY=:0 /home/youruser/.bin/battery-check

Setting the DISPLAY environment variable allows your scripts to run programs that require X. This cron job runs every 5 minutes. Also, make sure your crontab ends with an empty line by pressing enter.

Last edited by rodyaj (2014-02-02 23:49:10)

Offline

#3 2014-01-20 07:20:04

cris9288
Member
Registered: 2013-01-07
Posts: 348

Re: How can I make an udev rule for a low battery pop-up warning message?

rodyaj wrote:

Not all computers send events internally when the battery percentage changes, but maybe try adding env DISPLAY=:0 before /usr/bin/xmessage.

I use a cron job to run a script periodically. For example, install libnotify and then make a script /home/youruser/.bin/battery-check:

#!/bin/sh                                                          
                                                                      
battery_level=`acpi -b | cut -d ' ' -f 4 | grep -o '[0-9]*'`          
if [ $battery_level -le 10 ]                                          
then                                                                  
    notify-send "Battery low" "Battery level is ${battery_level}%!"   
elif [ $battery_level -le 3 ]                                         
then
    # your custom actions here e.g.,                                                                  
    # systemctl hibernate                                               
fi 

You may need to chmod +x the above script to make it executable (not sure).

Now edit the user crontab with crontab -e:

*/5 * * * * env DISPLAY=:0 /home/youruser/.bin/battery-check

Setting the DISPLAY environment variable allows your scripts to run programs that require X. This cron job runs every 5 minutes. Also, make sure your crontab ends with an empty line by pressing enter.

I think the script may need to know about the .Xauthority file,though I could be wrong.

Offline

#4 2014-01-20 10:15:46

mcloaked
Member
From: Yorkshire, UK
Registered: 2012-02-02
Posts: 914

Re: How can I make an udev rule for a low battery pop-up warning message?

rodyaj wrote:

Not all computers send events internally when the battery percentage changes, but maybe try adding env DISPLAY=:0 before /usr/bin/xmessage.

I use a cron job to run a script periodically. For example, install libnotify and then make a script /home/youruser/.bin/battery-check:

#!/bin/sh                                                          
                                                                      
battery_level=`acpi -b | cut -d ' ' -f 4 | grep -o '[0-9]*'`          
if [ $battery_level -le 10 ]                                          
then                                                                  
    notify-send "Battery low" "Battery level is ${battery_level}%!"   
elif [ $battery_level -le 3 ]                                         
then
    # your custom actions here e.g.,                                                                  
    # systemctl hibernate                                               
fi 

You may need to chmod +x the above script to make it executable (not sure).

Now edit the user crontab with crontab -e:

That is quite a nice way to do it - it would also be possible to amend the definition line for the battery_level variable using "upower -d", instead of acpi, and then piping that into analogous commands to strip out the level. In KDE there is already a power scheme to take action under different battery conditions, and within that you can also run scripts.  However if you are using a simpler D.E. then this kind of script is useful.


Mike C

Offline

#5 2014-01-20 12:34:30

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: How can I make an udev rule for a low battery pop-up warning message?

A script that runs as root, needs these 2 set, to use the GUI:

export DISPLAY=":0"
export XAUTHORITY=/home/yourusername/.Xauthority

Offline

#6 2014-01-20 17:27:47

mcloaked
Member
From: Yorkshire, UK
Registered: 2012-02-02
Posts: 914

Re: How can I make an udev rule for a low battery pop-up warning message?

brebs wrote:

A script that runs as root, needs these 2 set, to use the GUI:

export DISPLAY=":0"
export XAUTHORITY=/home/yourusername/.Xauthority

Another way to do it is to let the user be a "sudoer" with a restriction of which commands to use - then allow the user to execute that script? That is probably better than giving the user full root access unless you really trust the user (i.e. you are the user concerned!)


Mike C

Offline

#7 2014-01-20 18:11:39

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: How can I make an udev rule for a low battery pop-up warning message?

I was referring to a script that's run by udevd or acpid - they both run as root.

Offline

#8 2014-01-20 20:09:08

mcloaked
Member
From: Yorkshire, UK
Registered: 2012-02-02
Posts: 914

Re: How can I make an udev rule for a low battery pop-up warning message?

brebs wrote:

I was referring to a script that's run by udevd or acpid - they both run as root.

OK if you want to run things as root - though the following script seems to run happily as user without any need for root or sudo on my system in KDE:

#!/bin/bash                                                          

# Battery level warning and shutdown script
# Run this script from crontab every 5 minutes
# mc 20 Jan 2014

battery_level=`upower -d | grep percentage | grep -o '[0-9]*'`

if [[ "$battery_level" -le "20" && "$battery_level" -gt "10" ]]
    then
    notify-send "<font size=5 color=red><b><i>Battery low warning</i></b></font>"  "<br><font size=5 color=blue><b><i>Battery level is ${battery_level}%</i></b></font>"
elif [[ "$battery_level" -le "10" && "$battery_level" -gt "5" ]]
    then
    notify-send "<font size=5 color=red><b><i>Battery very low warning</i></b></font>"  "<br><font size=5 color=blue><b><i>Battery level is ${battery_level}%<br>Use ac power now,<br> or shutdown is imminent - close applications</i></b></font>"
else [ "$battery_level" -le "5" ]
    # our custom actions for critical battery level
    notify-send "<font size=5 color=red><b><i>Battery critical warning</i></b></font>"  "<br><font size=5 color=blue><b><i>Battery level is ${battery_level}%i<br>Shutting system down now</i></b></font>"
    sleep 5
    systemctl poweroff
fi

Obviously the parameters and notification messages will be changed to suit the needs of the user.

Last edited by mcloaked (2014-01-20 20:15:54)


Mike C

Offline

#9 2014-01-20 20:30:45

rodyaj
Member
Registered: 2009-10-13
Posts: 54

Re: How can I make an udev rule for a low battery pop-up warning message?

Just to clear things up for the original poster: the cron way doesn't necessarily need root, but it does need the DISPLAY variable exported to run graphical programs like xmessage or notify-send. If you want to use udev rules instead, then it may additionally require exporting XSESSION or using sudo, as well as exporting DISPLAY.

Also, the logic of the 'elif' was wrong in my original script because it would never execute, so I changed the script to have two separate if statements. Or just see mcloaked's script.

Last edited by rodyaj (2014-02-02 23:54:19)

Offline

#10 2016-12-07 15:09:11

ventto
Member
Registered: 2016-10-26
Posts: 5

Re: How can I make an udev rule for a low battery pop-up warning message?

Just to update the post and following the Rodyaj's answer.

If you want to run graphical programs like notify-send as non-root user (via root using 'su' for instance), you may need to set (at least):
- $DBUS_SESSION_BUS_ADDRESS
- $XAUTHORITY
- (optionaly) $DISPLAY

I had made a personal version using udev and libnotify (which works on Ubuntu as well), that I've finally pushed on github (cf. AUR|batify).

Last edited by ventto (2016-12-07 15:24:39)

Offline

Board footer

Powered by FluxBB