You are not logged in.

#1 2022-12-11 07:40:48

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Invoke notify-send through udev rules

Hello. I'm trying to make udev send me notifications concerning current status of battery, but I'm stumbled across some difficulties.
Here is an example of my udev rule:

SUBSYSTEM=="power_supply", ATTR{status}=="Not charging", RUN+="/usr/bin/sudo -u username -E notify-send -a 'udev' 'Battery is charged' 'Plug off the charger'"

I tried to run the exact same command as root and it works as it supposed, but when I plug in charger nothing happens. I checked status attribute and it has value "Not charging". What am I doing wrong?

Offline

#2 2022-12-11 08:06:40

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,421

Re: Invoke notify-send through udev rules

Preserving the environment won't work, because the relevant bits aren't there to be preserved.
https://wiki.archlinux.org/title/Udev#T … _udev_rule

Offline

#3 2022-12-11 08:21:26

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Re: Invoke notify-send through udev rules

seth wrote:

Preserving the environment won't work, because the relevant bits aren't there to be preserved.

Ok, but my dbus session file is not in the /run/user/1000/. It is somewhere in /tmp and always has random names, I suppose. How can I handle this?

Offline

#4 2022-12-11 08:44:53

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,421

Re: Invoke notify-send through udev rules

Why? How do you start the session?
Don't use "ly" nor "dbus-launch".

Otherwise, import the environment from a session process, see https://gist.github.com/AladW/de1c5676d93d05a5a0e1 for a **SAMPLE**.

Offline

#5 2022-12-11 08:52:00

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Re: Invoke notify-send through udev rules

seth wrote:

Why? How do you start the session?

I don't know. Wiki says that it starts automatically. I haven't changed anything. How am I supposed to start session?

Offline

#6 2022-12-11 08:55:41

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,421

Re: Invoke notify-send through udev rules

I don't know. Wiki says that it starts automatically.

What? What wiki? How did you install archlinux?
In case this is a slan barrier: "how do you log into your desktop environment?"

Offline

#7 2022-12-11 09:18:08

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Re: Invoke notify-send through udev rules

seth wrote:

What? What wiki?

This wiki: https://wiki.archlinux.org/title/D-Bus

seth wrote:

How did you install archlinux?

Just ordinary manual installation. It was before archinstall.

seth wrote:

how do you log into your desktop environment?

startx

Offline

#8 2022-12-11 09:34:43

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,421

Re: Invoke notify-send through udev rules

startx

Post your xinitrc and see the last link below.

Offline

#9 2022-12-11 09:44:28

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Re: Invoke notify-send through udev rules

seth wrote:

Post your xinitrc

$ cat .xinitrc
# init keyring
eval $(/usr/bin/gnome-keyring-daemon --start --components=gpg,pkcs11,secrets,ssh)
# export keyring
export GNOME_KEYRING_CONTROL GNOME_KEYRING_PID GPG_AGENT_INFO SSH_AUTH_SOCK

exec dbus-launch bspwm

Ahh, now I see, but even now I don't know how to start dbus properly. I will check your link.

Offline

#10 2022-12-12 06:43:13

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Re: Invoke notify-send through udev rules

seth wrote:

Post your xinitrc and see the last link below.

So, please, tell me how to start dbus, which will be connected with bspwm and its session file will be in /run/user/1000/ directory?

Offline

#11 2022-12-12 07:49:37

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,421

Re: Invoke notify-send through udev rules

You're not supposed to start dbus (I asked how you start the session - "startx" - the session *bus* is actually started automatically by systemd/logind)
In particular not using dbus-launch (so get rid of that)

What you need to do is to import the relevant environment into your X11 session, the relevant bit is the 2nd blue note in the wiki link.

Offline

#12 2022-12-12 09:38:27

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Re: Invoke notify-send through udev rules

seth wrote:

What you need to do is to import the relevant environment into your X11 session, the relevant bit is the 2nd blue note in the wiki link.

Sorry, I just don't understand where to look? What variables I need to import? And how to do it? Just

export DBUS_SESSION_BUS_ADDRESS

in .xinitrc? Maybe then I should export all variables in .xinitrc? Help me to understand this, please.

Offline

#13 2022-12-12 09:48:23

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,421

Re: Invoke notify-send through udev rules

The wiki, in the secoind note wrote:

Note: At the very least, ensure that the last if block in /etc/X11/xinit/xinitrc is present in your ~/.xinitrc file to ensure that the scripts in /etc/X11/xinit/xinitrc.d are sourced.

It's not about *exporting* anything, but *importing*…

Offline

#14 2022-12-12 10:46:34

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Re: Invoke notify-send through udev rules

seth wrote:

It's not about *exporting* anything, but *importing*…

Thank you. Now variables are ok, but I still don't get any notifications. Here is my rule:

SUBSYSTEM=="power_supply", ATTR{status}=="Not charging", RUN+="/usr/bin/su username -c /home/username/Scripts/charged.sh"

And here charged.sh:

#!/usr/bin/bash

export XAUTHORITY=/home/username/.Xauthority
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"

/usr/bin/notify-send -a 'udev' 'Battery is charged' 'Plug off the charger'

When this line as root everything works, but not when I plug in the charger.
What is wrong now?

Offline

#15 2022-12-12 10:55:14

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Re: Invoke notify-send through udev rules

I added a line:

/usr/bin/touch /home/username/Scripts/file

before notify-send and it doesn't create any files too.

Offline

#16 2022-12-12 13:16:20

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,421

Re: Invoke notify-send through udev rules

Where did you get that udev rule from? udevadm monitor?
Also, did you reload the udev rules?
And just to be sure: you did replace "username" w/ your actual username?

Offline

#17 2022-12-12 15:23:01

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Re: Invoke notify-send through udev rules

seth wrote:

Where did you get that udev rule from? udevadm monitor?

It is from /etc/udev/rules.d. I manually tried to trigger rules and they work. May be it is because udev don't trigger power_supply rules when I plug off or plug in charger. I read somewhere that udev trigger rules when battery capacity level changes, but is there another reasons it trigger these rules?

Offline

#18 2022-12-12 15:30:05

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,421

Re: Invoke notify-send through udev rules

It is from /etc/udev/rules.d

No, I meant "how did you come up w/ that rule"

Offline

#19 2022-12-13 07:48:37

Euler-Maskerony
Member
Registered: 2020-06-27
Posts: 30

Re: Invoke notify-send through udev rules

seth wrote:

No, I meant "how did you come up w/ that rule"

I don't know. I just thought that udev check attributes in every rule and if all of them match, then it triggers it. So, it is just logic.

Offline

#20 2022-12-13 08:01:46

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,421

Re: Invoke notify-send through udev rules

So, it is just logic.

Ex falso…

Check for the actual events w/ udevadm monitor and apply them accordingly.

Offline

Board footer

Powered by FluxBB