Ubuntu – Sudo -u -H gsettings set org.gnome.desktop.background picture-uri file:///path/to/file


I'm using Ubuntu 18.04.
The above command (and other gsetting command when used with sudo -u <user_name> -H) is/are showing a weird behavior.. (I'm expecting it to set the gsetting of the current user)

  • Firstly, after using the above command from terminal/script, if we try gsettings get the desktop background, it'll show as the picture we set it to previously, but the desktop background is not changed.
  • Second part is, if we log off and log-in (or even, open the background seiings in the settings UI and close it without making any changes) the gsettings get will now display back the normal value, i,e. the current display picture(not the set by us)

Please help me understand this behavior, and what can I do to make it set the gsettings to the current user (using sudo).
Please refer: Trouble with handling sudo
Thanks a loads!

Edit 1:
Just figured out this is happening coz of missing an env variable DBUS_SESSION_DBUS_ADDRESS. When we are using sudo -Hu <user> env > text.txt we get a very limited subset of env varables.. in which the above variable is missing.. From: Background not changing using gsettings from cron

Now, my question boils down to how to add that env variable while I'm setting gsettings through python script! OR is there any other better way of doing it?

Best Answer

Related Question