1

I have a script, written in python:

#!/usr/bin/python3
from tkinter import messagebox
variable = messagebox.showinfo('title', 'question')

This python script does nothing else than showing a message box on my screen. I want to start that script whenenver I connect an usb drive to my computer.

So I created a new file 10-myself.rules in the directory /etc/udev/rules.d/.

KERNEL="sdb",RUN+="/usr/bin/python3 /home/user/projects/path_to_python.py"

But I do not see any message box when put the usb drive into my computer. Where is the error? How can I debug that?

I also called the command

udevadm control --reload-rules

And I tried to change the rule to

KERNEL="sdb*",RUN+="/usr/bin/python3 /home/user/projects/path_to_python.py"

or to the rule

KERNEL="sdb1",RUN+="/usr/bin/python3 /home/user/projects/path_to_python.py"

If I change the script to

file1 = open("/home/user/udev.txt", "w")
file1.write("Hello")
file1.close()

then this script works.

devopsfun
  • 1,407

2 Answers2

1

I'd guess the code runs as root, or a udev user, and that these users do not have authority to chat with your window manager and make Tk windows or such. Some form of communication might help, e.g. the udev script write log entries, or sends messages to a socket, that on the other side of something in the window manager environment can read messages from and take appropriate actions.

thrig
  • 34,938
1

When you run the script from an interactive shell, you have a DISPLAY environment variable indicating a server which you have authority to connect to.

When it is run, by udev, that is not the case.

In general, you don't want udev to do anything that depends on having X11 up and running, and you certainly don't want to increase its attack surface in that way.

It appears that you're looking for a subscription/notification system, so you can start the listener (as yourself) with your X session, and have the notifier run from udev. You may want to look into DBus or UDisks for this; alternatively, you could start inotifywait watching for the creation of the device file, and read its events in your script (again, all as user, not root).

Toby Speight
  • 8,678