Наконец, я создал клиент dbus, запущенный от имени пользователя, который получает сигнал от системной шины и показывает уведомление пользователю.
Код клиента dbus находится вhttps://github.com/AlmuHS/Pendrive_Reminder/blob/work-in-progress/dbus-client/client.py
В скрипт send-notify.sh
я добавил только
dbus-send --system /org/preminder/mensaje org.preminder.App
При выполнении клиента dbus от имени пользователя уведомление отображается правильно
Теперь я пытаюсь сделать так, чтобы клиент запускался автоматически, когда пользователь подключает флешку
Продолжить в Как запустить клиент dbus из скрипта?
polkit (ypkexec
)eliminan las variables de entorno DISPLAY
y XAUTHORITY
que se necesitan para el acceso X. notify-send
falla porque no puede acceder a la pantalla.
De la página de manual de pkexec:
As a result, pkexec will not allow you to run X11 applications as another user since the $DISPLAY and $XAUTHORITY environment variables are not set. These two variables will be retained if the org.freedesktop.policykit.exec.allow_gui annotation on an action is set to a nonempty value
No estoy familiarizado con polkit; tal vez pueda configurar org.freedesktop.policykit.exec.allow_gui
solo para esta regla, o hay otras posibilidades. Lo siento, no puedo proporcionar una solución lista.
Sin embargo, el punto central es proporcionar DISPLAY
y XAUTHORITY
para notify-send
.
(No me golpees :una solución sucia sería codificar DISPLAY=:0
y XAUTHORITY=...
en tu script de notificación. Tenga en cuenta que esto puede fallar si algo cambia ).
Editar :Basado en la discusión anterior, una solución alternativa que debería funcionar con múltiples usuarios y sinXAUTHORITY
:
En el inicio de sesión X,un script debe ejecutarse automáticamente (tal vez alguna configuración .desktop
en~/.config/autostart
):
#! /bin/bash
# allow polkitd access to X.
# xhost is an alternative to XAUTHORITY authentication
xhost +SI:localuser:polkitd
# store DISPLAY for each user
echo $DISPLAY > /tmp/display.$USER
En su script polkit incluya
export DISPLAY=$(cat /tmp/display.$user)
@mviereck
Intenté crear un archivo de política de polkit para notificar -enviar, con este contenido
<policyconfig>
<action id="org.freedesktop.notify-send">
<description>Launch notify-send command</description>
<message>Authentication is required to run the gedit</message>
<icon_name>accessories-text-editor</icon_name>
<defaults>
<allow_any>yes</allow_any>
<allow_inactive>yes</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/notify-send</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
</action>
Pongo este archivo en/usr/share/polkit-1/actions/org.freedesktop.policykit.notify-send.policy
Pero, después de presionar el botón de apagado, el menú de apagado tardó mucho en mostrarse y no apareció la notificación