1

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?

gaia
  • 11

1 Answers1

0
  • As a long learning process for me, I was supporting such hacks. Like the one your script probably needs for DBus address .., see this answer:

    How to pause VLC playback when the headphones are disconnected?

  • Udev action commands are run within a sandbox. So it is missing desktop environment variables and services. So Udev rules are not attended to be used and connected directly to GUI and User environment.

    To be complete, check Wikipedia - Udev for its objectives.

    I would recommend doing it the same way as any user application developer should do .. Use a library. I don't care which one: a low level one for udev or a high level one like udisks2 (for GTK's DE),...

  • If you look at that hack from system design prospective, you will see how ugly it is. How a low level system could relay on high level one!.

user.dz
  • 48,105