Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activating a script using udev when power supply is connected/disconnected

Tags:

linux

udev

I'm trying to get udev to run a couple of small scripts when I connect/disconnect the powersupply. I have the following code in /etc/udev/rules.d/50-caff.rules :

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_STATUS}=="Charging", RUN+="/home/haukur/rules/off.sh"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_STATUS}=="Discharging", RUN+="/home/haukur/rules/on.sh"

Here is on.sh:

#!/bin/sh
caffeine -a

and off.sh:

#!/bin/sh
caffeine -d

Anyway, I wrote these, wrote udevadm control --reload-rules into bash and... nothing happened. caffeine doesn't appear to activate at all when I plug or unplug the power supply.

According to /var/log/syslog (Ubuntu's replacement for /var/log/messages) udev recognizes when I pull the plug:

Feb 26 08:44:52 (none) udevd[3838]: starting '/home/haukur/rules/off.sh'

but when it tries to run off.sh (which itself tries to run caffeine), it returns this error:

udevd[2719]: '/home/haukur/rules/off.sh'(err) '** (caffeine:3840): WARNING **: Command line `dbus-launch --autolaunch=62907743a139af9b3c86412e00000026 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n'

Do you know any way to get around this? Running Ubuntu 12.04 LTS with xmonad WM.

like image 314
Hawkuro Avatar asked Nov 13 '22 10:11

Hawkuro


1 Answers

If the application "caffeine" needs to access you desktop, you probably need to export the DISPLAY before calling the program:

export DISPLAY=:0 

You may simply prepend this to the command invocation:

DISPLAY=:0 caffeine -a
like image 161
Raúl Salinas-Monteagudo Avatar answered Dec 11 '22 10:12

Raúl Salinas-Monteagudo