I followed the instructions here to set up an automated backup, such that whenever I connect a certain external drive to the computer, the backup is automatically started.
This works, but in addition I wanted to show two zenity
dialogs to the user:
- before backup is run to ask if the user really wants to do the backup (or just use the drive)
- when backup is done to ask if the user wants to safely remove the drive (or use the drive)
Now my problem is that the backup script from the borg site is run as root
. To be able to show the dialogs to the user, I added --display=:0.0
to the zenity commands and added this to my ~/.bashrc
:
xhost local:$USER > /dev/null
The problem with this approach is: it only works once a terminal (i.e. the Gnome app) was started. If I just log into Gnome and plug in the drive, no dialog appears and I can read in sudo journalctl
that the script failed because of this error:
zenity[2731]: cannot open display: :0.0
But if I open a terminal first, it works. I also tried adding the xhost
command under /etc/profile.d/
but that didn't work even if a terminal was started.
So my question is: Where do I need to put the xhost
command to make this work even if the user doesn't open the terminal app? Or how can it be done?
~/.bashrc
or/etc/profile.d/*
, neither of which is used by the GUI--there are only processed when you log in to a text based login, such as a terminal window. Read your shell's documentation. You will also have the issue of "what if no one is logged in when I plug the device in?" What you probably want is a udev trigger that can run a script or program to first check if a user is logged in to the GUI, and if so, then send a notification (you will have to ask another question specific to your setup for how to do this...)