Running gsettings as root

Oliver Grawert ogra at ubuntu.com
Wed Aug 17 13:56:32 UTC 2016


hi,
On Mi, 2016-08-17 at 15:09 +0200, Josef Wolf wrote:
> On Wed, Aug 17, 2016 at 02:37:50PM +0200, Oliver Grawert wrote:
> > 
> > On Mi, 2016-08-17 at 11:21 +0200, Josef Wolf wrote:
> > > 
> > > 
> > > I want to do lots of configuration settings as root in the
> > > background
> > > I know, I can do this with
> > > 
> > >   sudo -u $USER dbus-launch --exit-with-session gsettings set [
> > > .... ]
> > > 
> > > This works fine while the user is NOT logged in.
> > > 
> > > But it won't work if the user IS logged in.
> > 
> > have a look at the dconf command (dconf-cli package), IIRC dbus
> > will
> > check if there is a gsettings-daemon running and block if there is
> > one
> > (to prevent concurrent access to the same key) ... 
> > 
> > dconf can modify the db directly without involving the daemon.
> 
> Are you sure about this? The dconf man page says:
> 
>   Note that dconf needs a D-Bus session bus connection to write
> changes
>   to the dconf database.
> 
hmm, you are right ... the check is in the tool itself and both use
dbus ... 
nontheless, dbus-launch is definitely the wrong way since that spawns a
dbus daemon with a new session ID but what you want is to connect to
the running session bus in case the user is logged in ...

so use dconf directly, but make sure the right session ID is in your
environment ... i fear you need a slightly bigger script for that, the
session ID is stored in: /run/user/$UID/dbus-session ... you can source
that file and then just export DBUS_SESSION_BUS_ADDRESS, when you then
call dconf it will attach to the running session bus ... if
/run/user/$UID/dbus-session does not exist you fall back to use dbus-
launch to actually create a fresh session (since the user is not logged
in of that file doesnt exist)

here is a script snippet that we use on the phone images to do
something similar:

phablet at ubuntu-phablet:~$ cat /etc/profile.d/dbus-source.sh 
# source dbus address from new location
[ -e $XDG_RUNTIME_DIR/dbus-session ] && . $XDG_RUNTIME_DIR/dbus-session
[ -n "$DBUS_SESSION_BUS_ADDRESS" ] && export DBUS_SESSION_BUS_ADDRESS

(note that XDG_RUNTIME_DIR translates to /run/user/$UID ... but you
will likely not have it set when you run from "sudo -u $USER", so you
need to obtain the UID somewhere else and use the full path)

ciao
	oli
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: This is a digitally signed message part
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20160817/a5efcf5e/attachment.sig>


More information about the ubuntu-users mailing list