When I run the following command, everyhting works as expected. No error and I get a system notification saying "Hello":
$ python3
>>> import os
>>> os.system("notify-send Hello")
0
However, when I do this:
$ sudo python3
>>> import os
>>> os.system("notify-send Hello")
The script gets stuck and nothing happens.
I then tried to do this:
$ sudo python3
>>> import os
>>> os.seteuid(1000)
>>> os.system("notify-send Hello")
(1000
being my normal non-root user account)
But still, the script gets stuck and nothing happens.
I also tried this:
$ sudo python3
>>> import os
>>> os.system("su my-user-name -c 'notify-send Hello'")
and this:
$ sudo python3
>>> import os
>>> os.seteuid(1000)
>>> os.system("su my-user-name -c 'notify-send Hello'")
They all have the same issue...
I'm not looking for an alternative way of creating notifications. I'm not interested in subprocess
or things like notify2
which cause a whole new category of problems on my system. Oh and please don't tell me not to use sudo. I have my reasons.
The implementation detail I've discovered through trial-and-error is
notify-send
requires theXDG_RUNTIME_DIR
environment variable to function -- at least with these versions:I first determined it needed some sort of environment variable by using
env -i notify-send hello
, which produced no notification.I then bisected the environment with a modified version of this script
How you get that environment variable is up to you, but you need to be running
notify-send
as the proper user and with that variable set.Here's a sample python script, I refuse to use
os.system
due to its security issues:DISCLAIMER: this script is doing some very hacky things that I wouldn't necessarily suggest in production code. Notably:
sudo
Sample usage: