0

In a recent QA I figured out how to set custom shortcuts using gsettings set. Now I am trying to automatically perform those settings at first boot.

I have set up /etc/rc.local to run some script every time the laptop boots, as advised by this answer modified so that it only actually runs if it has never run before.
The same script works when run as the user from a terminal, but I haven't yet figured out how to make it have an effect when run from rc.local.

I assume the problem has to do with the fact that rc.local is run as root and not as the unprivileged user (that is called generic on my machine).

This answer advises to use sudo -H -u generic <THE ACTUAL COMMAND>.
When I run my script (with set -x) from the terminal, it seems to run successfully and when I gsettings get the values I modified, I get the value back that I have set this way. But once I open the Settings > Devices > Keyboard and scroll down to the custom shortcuts, it will show the previously set values (or nothing, if it was not set). At this point in time, querying the same key again using the same terminal as before will also return the old value.
Running any of the commands in my script directly in the terminal will produce additional output though:

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:~$

The file /home/generic/.cache/dconf/user exists but it belongs to root:root and has permissions -rw-------.
Taking ownership using sudo chown generic:generic /home/generic/.cache/dconf/user fixes this symptom (I don't know why it was that way). But still, setting the binding using the command above does not have any effect on neither the values displayed in the Gnome Settings GUI nor the effect it should have (Modifying the keyboard shortcut that triggers some custom command). Not even when I reload gnome using AltF2 and enter "r".
Running the same command without sudo from a terminal instantly has the desired effects.

How can I run my gsettings set commands at the first startup of my ubuntu 18.04 just as if it were the (only) user generic running it?


The relevant part of my script is this:

#!/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/']"
2
  • 1
    Are you trying to modify settings for a particular user, or eventually for all users? perhaps this is something you could approach using dconf profiles instead? May 4, 2020 at 20:42
  • I'm fine with either option, since each machine will only have one user @steeldriver . This is the first time I hear of dconf profiles - will look them up later. Thanks for suggesting
    – lucidbrot
    May 5, 2020 at 5:28

0

You must log in to answer this question.

Browse other questions tagged .