You are not logged in.

#1 2018-06-20 19:52:18

hakunamatata
Member
Registered: 2016-03-31
Posts: 36

[SOLVED] Referencing environment variables in a udev rule

I have a udev rule where I use environment variables (e.g. $USER):

SUBSYSTEM=="power_supply", \
ATTR{status}=="Discharging", \
ATTR{capacity}=="[0-5]", \
ENV{DBUS_SESSION_BUS_ADDRESS}="unix:path=/run/user/$UID/bus", \
RUN+="/usr/bin/su $USER -c '/usr/bin/notify-send --urgency=critical \"Low Battery\" \"$attr{capacity}% remaining\"'"

Currently, I have the actual values hardcoded, but I would like to reference the environment variables. Does anyone know how to achieve this?

Last edited by hakunamatata (2018-06-22 09:26:00)

Offline

#2 2018-06-20 20:38:25

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] Referencing environment variables in a udev rule

You put them in a script.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2018-06-20 20:50:26

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,427

Re: [SOLVED] Referencing environment variables in a udev rule

udev rules are executed as root, these won't resolve to your user correctly.

Online

#4 2018-06-20 21:27:28

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: [SOLVED] Referencing environment variables in a udev rule

The batify AUR package uses the xpub script to extract the information about the currently active user. xpub or some similar script is the only way you can use user specific information in udev.
https://aur.archlinux.org/packages/xpub/
https://aur.archlinux.org/packages/batify/

Last edited by progandy (2018-06-20 21:29:23)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#5 2018-06-20 22:05:03

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] Referencing environment variables in a udev rule

 # grab user details from the environment                                                                                                
   arr=(
     $(strings /proc/$pids/environ |\
     awk -F'=' '/^USER/{user=$2};\
     /^DISPLAY/{display=$2}; /^DBUS/{bus=$3}\
     END {print user"\n"display"\nunix:path="bus}')
     )
   user="${arr[0]}"
   dply="${arr[1]}"
   dbus="${arr[2]}"

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2018-06-21 19:12:46

hakunamatata
Member
Registered: 2016-03-31
Posts: 36

Re: [SOLVED] Referencing environment variables in a udev rule

I was hoping for a "cleaner" solution. I think I will stick with the hardcoded solution for now. Thank you all anyway.

Offline

Board footer

Powered by FluxBB