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?

  • Possible duplicate of script launched from udev rule not displayed in terminal? – WinEunuuchs2Unix Sep 30 '18 at 14:49
  • Please review the duplicate candidate. If it doesn't solve your problem make a comment here addressed to @WinEunuuchs2Unix and I'll retract my close vote. Thank you. – WinEunuuchs2Unix Sep 30 '18 at 14:50
  • @WinEunuuchs2Unix I tried to run the python script and "notify-send whatever" comand, the first give me the same error, the second simply does not work. – gaia Sep 30 '18 at 22:37
  • Thanks for trying the other solution. I've retracted my close vote and hope someone knowledgeable in Python answers your question. – WinEunuuchs2Unix Sep 30 '18 at 23:00
  • Thanks for suggestion, maybe I'm forgetting something that is implicit in @b_laoshi 's solution, for example the script must be run from the same user? – gaia Oct 1 '18 at 7:42
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!.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.