notify-send command doesn't launch the notification through systemd service
After asking a colleague, it seems that the DBUS_SESSION_BUS_ADDRESS
variable is not available when script is run from service. Just adding the export
command as follow to the script is enough:
#!/bin/bash
export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-unix:path=/run/user/${UID}/bus}"
notify-send "Hello World"
Apparently, in the service file, Environment
is not necessary.
notify-send not working (in script) executed from udev
The main problem is, that the udev-rule will not run in any xorg-related environment per default, thus not knowing which DISPLAY to use. Therefore it will always fail, if You want to echo something into a terminal like a gnome-terminal for example. The script, which shall be executed on the udev-rule-match, must prior to any ui-related execution first export the DISPLAY.
This is done via
export DISPLAY=:0
I assume, that this also will be the problem, and notify-send will just run against the wall.
I am actually also playing with udev-rules, and I managed it to work, though i am acting as root, similar to my answer and this one found already here :
https://unix.stackexchange.com/questions/80882/udev-running-a-shellscript-that-accesses-an-x-display
And also here
Scripts launched from udev do not have DISPLAY access anymore?
You might want also to check zenity. Very helpful for small notifications
The command of 'notify-send' does not work in supervisor
Proper environment variables need to be set (DISPLAY & DBUS_SESSION_BUS_ADDRESS). You can do it in many different ways, depending on your needs, like e.g.
a) per subprocess
import os
os.system('DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send hello')
b) in script globally
import os
os.environ['DISPLAY'] = ':0'
os.environ['DBUS_SESSION_BUS_ADDRESS'] = 'unix:path=/run/user/1000/bus'
os.system('notify-send hello')
c) in supervisor config per program
[program:test_notify]
;
; your variables
;
user=john
environment=DISPLAY=":0",DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
The above examples have a couple of assumptions (you may want to change these settings accordingly):
- script is run as user john
- UID of user john is 1000
- notification appear on display :0
To run script as root and show notification for regular user, use sudo as described on Arch wiki Desktop_notifications.
Related Topics
Forcing a Context Switch from The Userland on Linux
How Convert Address in Elf to Physical Address
Problems with Sudo Inside Expect Script
Why Does The Stack Have to Be Page Aligned
Use "Git Revert" to Back-Out a Change Adding a Line
How to Install Pysqlite for Python3.4.2
Linux Mail Adding Content Type Headers Not Working
How The Buffering Work in Socket on Linux
Parsing Result of Diff in Shell Script
Iptables Remove Specific Rules by Comment
How to Quickly Check If Linux 'Unzip' Is Installed Using Perl
Pid File Exists, But Process Is Not Running
Avoid Copying of Data Between User and Kernel Space and Vice-Versa
End Perl Script Without Waiting for System Call to Return
Understanding Glibc Malloc Trimming
Store Passwords Required by a Linux Daemon
When Compiling Programs to Run Inside a Vm, What Should March and Mtune Be Set To