File /etc/udev/rules.d/90-test.rules:

ACTION=="add", SUBSYSTEM=="usb", ENV{DISPLAY}=":0", ENV{HOME}="/home/user", RUN+="/etc/udev/rules.d/test.sh"

File /etc/udev/rules.d/test.sh:

#!/usr/bin/env bash
/usr/bin/kcalc &

When I plug in an USB device, kcalc starts as expected (2 times because the rule is kept simple). But after 4 to 5 seconds, kcalc closes again. I also tried it with nohup but without success. Same occurs for other applications like konsole and kate.

When I don't run kcalc in the background (no &), it doesn't close but it seems like further processing is blocked. The Device Notifier notifies me only after I closed kcalc. I dislike this solution because it affects other parts of the system.

So why do the applications close and how can I prevent it?


From the udev manpage

Starting daemons or other long-running processes is not appropriate for udev; the forked processes, detached or not, will be unconditionally killed after the event handling has finished.

Thus, the best way is to use udev to trigger a systemd service that ultimately starts your script, as in

RUN{program}="/bin/systemctl start my_service.service"

where you have to write the appropriate .service file.

| improve this answer | |

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