当前位置: 首页>>技术教程>>正文


如何通过远程 shell 更改 Gsettings?

, , , ,

问题描述

我需要通过 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

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

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

我能做些什么?

最佳方法

关键是设置DBUS_SESSION_BUS_ADDRESS环境变量。

this thread 上,我找到了以下脚本,它有助于获取该变量的正确值。它需要在桌面上运行的进程的名称,我们要在其上更改 dbus 设置。 (可以有超过 1 个并行运行的图形会话)。让我们称之为 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 '\0' '\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"

一切正常。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/13601.html,未经允许,请勿转载。