xrandr come root non riesce a trovare display


9

Ho la seguente regola che chiama il mio script quando aggancio o disancoraggio del ThinkPad:

# /lib/udev/rules.d/81-thinkpad-dock.rules
KERNEL=="dock.0", ATTR{docked}=="0", RUN+="/usr/bin/think-dock-hook off"
KERNEL=="dock.0", ATTR{docked}=="1", RUN+="/usr/bin/think-dock-hook on"

Questo è lo script che verrà chiamato:

# /usr/bin/think-dock-hook
# Find the user who is currently logged in on the primary screen.
user="$(who -u | grep -F '(:0)' | head -n 1 | awk '{print $1}')"
su -c "bash -x /usr/bin/think-dock $setto" "$user" >> /root/think-dock.log 2>&1 &

E la sceneggiatura che viene quindi chiamata fa qualcosa con xrandr.

Il fatto è che posso eseguire think-dock oncome il mio utente ( mu) e funziona. Posso sudo -ie corro think-dock-hook one funziona anche. Ma quando lo lascio udeveseguire, viene visualizzato il seguente errore xrandr:

# output of bash -x think-dock on
+ xrandr --output LVDS1 --auto
Can't open display

Ora se chiamo xrandrdalla mia sudo -ishell, ottengo:

No protocol specified
No protocol specified
Can't open display :0

Tuttavia, se lo faccio su -c xrandr mudalla mia sudo -ishell, ottengo l'output previsto.

Quindi non capisco davvero, la sceneggiatura chiamata da udevfallisce.


1
grep -F '(:0)'dovrebbe essere grep -F '(:0.0)'; prova anche ad attaccare un DISPLAY=:0.0davanti a/usr/bin/think-dock
— ish il

Questo DISPLAYsembra buono. I miei whodisplay però (:0). Quindi non penso che grep sarebbe meglio così.
— Martin Ueding,

Va bene, con il DISPLAYdavanti bash, sembra funzionare ora. Grazie!
— Martin Ueding,

Grande! Il mio whoaveva 0.0, ma comunque. Dal momento che ha funzionato, aggiungerò la DISPLAYparte come risposta; per favore accettalo. Grazie!
— ish

Risposte:


7
  • xrandr deve sapere di quale display stai parlando, in genere tramite la DISPLAYvariabile d'ambiente
  • root(che udev funziona come) non ha un DISPLAYset predefinito ; anche se lo ha fatto, su -cnon preserva l'ambiente per impostazione predefinita
  • Quindi passalo esplicitamente a bash e questo dovrebbe risolvere il tuo problema, ad esempio:

    su -c "DISPLAY=:0.0 bash -x /usr/bin/think-dock $setto" "$user"
    
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.