如何从 root shell 运行 gsettings?

如何从 root shell 运行 gsettings?

在一个最近的质量保证我弄清楚了如何使用 设置自定义快捷方式gsettings set。现在我尝试在首次启动时自动执行这些设置。

我已经设置/etc/rc.local为每次笔记本电脑启动时运行一些脚本,按照这个答案经过修改,只有以前从未运行过时它才会真正运行。
以用户身份从终端运行相同的脚本时会起作用,但我尚未弄清楚如何使其从运行时产生效果rc.local

rc.local我认为问题在于以 root 身份运行而不是以非特权用户身份(在我的计算机上调用generic

这个答案建议使用sudo -H -u generic <THE ACTUAL COMMAND>。当我从终端
运行我的脚本(使用)时,它似乎成功运行,当我输入修改的值时,我会得到以这种方式设置的值。但是一旦我打开并向下滚动到自定义快捷方式,它将显示之前设置的值(如果未设置,则不显示任何内容)。此时,使用与之前相同的终端再次查询相同的键也将返回旧值。 直接在终端中运行我的脚本中的任何命令都会产生额外的输出:set -xgsettings getSettings > Devices > Keyboard

generic@segelbrot~$ sudo -Hu generic gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding '<Primary><Super>Right'

(process:5633): dconf-CRITICAL **: 22:05:08.571: unable to create file '/home/generic/.cache/dconf/user': Permission denied. dconf will not work properly.

(process:5633): dconf-CRITICAL **: 22:05:08.572: unable to create file '/home/generic/.cache/dconf/user': Permission denied. dconf will not work properly.

(process:5633): dconf-CRITICAL **: 22:05:08.592: unable to create file '/home/generic/.cache/dconf/user': Permission denied. dconf will not work properly.
generic@segelbrot:~$

该文件/home/generic/.cache/dconf/user存在,但它属于root:root并具有权限-rw-------
使用 获取所有权sudo chown generic:generic /home/generic/.cache/dconf/user可修复此症状(我不知道为什么会这样)。但是,使用上述命令设置绑定对 Gnome 设置 GUI 中显示的值或应有的效果(修改触发某些自定义命令的键盘快捷键)没有任何影响。即使我使用 重新加载 gnomeAltF2并输入“r”时也没有。
从终端运行相同的命令而不使用 sudo 会立即产生预期的效果。

如何gsettings set在第一次启动 ubuntu 18.04 时运行我的命令,就像是(唯一)用户generic在运行它一样?


我的脚本的相关部分是这样的:

#!/bin/bash
# This is supposed to be run as root on first boot by placing it in /etc/rc.local
# It automatically exits if it has been run before, by detecting the presence of the .canary.done file
# Logs to stdout.
set -x
CANARYFILE="/install/firstboot.canary.done"
if [ ! -f $CANARYFILE ]; then
    echo '---'
    echo '--- STARTING FIRSTBOOT.sh ---' 
    echo '---'
    # create canary file so that we won't run again the next boot
    touch $CANARYFILE
else
    exit 1
fi

# --- actual script contents below this line ---
USERNAME='generic'

# -- setup gnome shortcuts --
# install wmctrl to allow moving windows to a different GNOME workspace without switching to that workspace as per my askubuntu questions
apt-get -y install wmctrl
# and set up the settings... but as the user, not as root.
sudo -Hu $USERNAME gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding '<Primary><Super>Left'
sudo -Hu $USERNAME gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name 'move to first workspace'
sudo -Hu $USERNAME gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command 'bash /opt/workspacemagic/send_to_zeroeth.sh'
sudo -Hu $USERNAME gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding '<Primary><Super>Right'
sudo -Hu $USERNAME gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ name 'move to next workspace'
sudo -Hu $USERNAME gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ command 'bash /opt/workspacemagic/send_to_next.sh'
# and link to them. This OVERWRITES the array.
#  important: paths start and end with a slash
sudo -Hu $USERNAME gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/']"

相关内容