dconf如何通过远程 shell 更改 Gsettings?

分享于 

3分钟阅读

ubuntu

  繁體

问题

需要通过Puppet,虚拟终端或ssh自动配置桌面。

不幸的是,通过ssh或虚拟终端调用gsettings给出:


gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ hsize"4"



(process:29520): dconf-WARNING **: failed to commit changes to dconf: Cannot autolaunch D-Bus without X11 $DISPLAY



当我用$DISPLAY设置export DISPLAY=:0.0时,它给出另一个错误:


(process:29862): dconf-WARNING **: failed to commit changes to dconf: Could not connect: Connection refused




答案1

关键是设置DBUS_SESSION_BUS_ADDRESS环境变量。

discover_session_bus_address.sh


#!/bin/bash



# Remember to run this script using the command"source ./filename.sh"



# Search these processes for the session variable 


# (they are run as the current user and have the DBUS session variable set)


compatiblePrograms=( nautilus kdeinit kded4 pulseaudio trackerd )



# Attempt to get a program pid


for index in ${compatiblePrograms[@]}; do


 PID=$(pidof -s ${index})


 if [["${PID}" !="" ]]; then


 break


 fi


done


if [["${PID}" =="" ]]; then


 echo"Could not detect active login session"


 return 1


fi



QUERY_ENVIRON="$(tr '' 'n' < /proc/${PID}/environ | grep"DBUS_SESSION_BUS_ADDRESS" | cut -d"=" -f 2-)"


if [["${QUERY_ENVIRON}" !="" ]]; then


 export DBUS_SESSION_BUS_ADDRESS="${QUERY_ENVIRON}"


 echo"Connected to session:"


 echo"DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}"


else


 echo"Could not find dbus session ID in user environment."


 return 1


fi



return 0



使用这个脚本,我们可以执行以下操作,假设unity进程正在桌面上运行,我们想应用设置:


. ./discover_session_bus_address.sh unity


gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ hsize"4"



一切正常。


答案2

我在尝试通过SSH为vagrant镜像进行gsettings变更时遇到了同样的问题。


答案3

我有一个POST-Install脚本设置gsetting。

以下是方法:


#!/usr/bin/env bash


# Get the Real Username


RUID=$(who | awk 'FNR == 1 {print $1}')



# Translate Real Username to Real User ID


RUSER_UID=$(id -u ${RUID})



# Set gsettings for the Real User


sudo -u ${RUID} DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/${RUSER_UID}/bus" gsettings set org.gnome.desktop.interface clock-show-date false



exit