My goal is to trigger a notification with custom actions when a specific storage device is mounted. Notifications are made in python with pynotify
I run lsusb
to get device's idVendor and idProduct parameters and i create a udev rule /etc/udev/rules.d/100-mount-notifybackup.rules
with this content:
ACTION=="add", SUBSYSTEM=="block", ATTR{partition}=="*", ATTRS{idVendor}=="0xxx", ATTRS{idProduct}=="1xxx", TAG+="systemd", ENV{SYSTEMD_WANTS}+="notify_backup.service"
When the external storage is attached the rule start a systemd service(/etc/systemd/system/notify_backup.service
)
[Unit]
Description=notify backup
[Service]
User=your_user
Group=your_user
Type=oneshot
RemainAfterExit=no
ExecStart=/home/your_user/bin/notifybackup.sh
[Install]
this service execute notifybackup.sh
that run the python script
#!/bin/bash
script_name=$0
script_full_path=$(dirname "$0")
/usr/bin/python -v $script_full_path/notify.py > /home/your_user/fileName.txt 2>&1
Python script:
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify
from subprocess import call
#call('ls')
#Notify.init("App Name")
#Notify.Notification.new("Hi").show()
def ignore_cb():
return None
Notify.init("Test Capabilities")
caps = Notify.get_server_caps()
if caps and 'actions' in caps:
# We support actions, so add a button.
notification = Notify.Notification.new("Can we use actions?", \
"Yup, we sure do.")
notification.add_action("ignore", "Ignore", ignore_cb)
else:
notification = Notify.Notification.new("Can we use actions?", \
"Nope, we don't support actions.")
notification.show()
The notification appear when i run bash script but it give me this error when i insert the storage device:
(process:9357): libnotify-WARNING **: 16:37:48.548: Failed to connect to proxy
Traceback (most recent call last):
File "/home/gaia/bin/notify.py", line 23, in <module>
notification.show()
GLib.Error: g-io-error-quark: Cannot autolaunch D-Bus without X11 $DISPLAY (0)
is it trying to run python script in a shell?
DISPLAY=:0
and delay actions until user signs on and then set variableXAUTHORITY=
. I'm not sure what Python libraries are needed to do this though. – WinEunuuchs2Unix Oct 01 '18 at 10:37