Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I wrote a script to change my desktop background. I want this script to run on anacron because I often turn my computer off at night. I've tested parts of the script, and I think the problem is with this line:

DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '"file://'$wallPath'"'

($wallPath is the wallpaper path)

It switches my wallpaper but it doesn't when anacron runs. I figured this was because anacron runs as root (I also check dconf for root and my user to check this). So I hacked up a way to run anacron as a specific user. But that didn't work.

Can anyone help me figure out a way to run gsettings as a user? I've tried su (username) -c 'sh run/this/script' to no avail. I've found some stuff about exporting some dbus env variables, but I'm having difficulty following it.

share|improve this question

3 Answers 3

As root, a simple sudo -u <username> <command> will do.

share|improve this answer
    
this is one of the first things i tried. unfortunately, it doesnt work. –  yoshi Dec 31 '12 at 22:44

You can set up a separate anacron instance to run in user mode. To do so create a .anacron folder in your home directory and in it two subfolders, etc and spool.

Then proceed to create a new anacrontab in your newly created ~/.anacron/etc folder with the following contents:

# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

1   10    wallpaperchanger script.sh

where script.sh is the path to your script (or a one-liner, your choice). Configure the intervall and delay to your pleasure and save the file as anacrontab.

Then add the following line to your ~/.profile file:

/usr/sbin/anacron -s -t ${HOME}/.anacron/etc/anacrontab -S ${HOME}/.anacron/spool

Anacron should be up and running on next login.

Note: make sure $wallPath is a sane file path as you didn't account for spaces and special characters in your script.

share|improve this answer
    
So I have this set up. I know the script is working. But the gsettings set command isnt working. Do you know of any place I can see the gsettings log? –  yoshi Jan 1 '13 at 15:45
    
Well, you could redirect the STDOUT of your command to a logfile, e.g. by adding GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '"file://'$wallPath'"' > "$HOME/log.txt" –  Glutanimate Jan 1 '13 at 16:32
    
@yoshi Also, I just noticed that I have a similar script I haven't used for a while. But I think I remember it working quite well back in the day. This is it: export DISPLAY=:0; GSETTINGS_BACKEND=dconf /usr/bin/gsettings set org.gnome.desktop.background picture-uri $URI –  Glutanimate Jan 1 '13 at 16:37
    
okay so i see what the problem is. I'm having the problem listed here: bugs.launchpad.net/ubuntu/+source/nautilus/+bug/1033034 As soon as I hit 'change wallpaper' the background changes. gsettings are set appropriately and everything. just the background doesn't change. I've tried adding DISPLAY and GSETTINGS_BACKEND declarations before gsetting, but it still won't switch automatically –  yoshi Jan 2 '13 at 4:18
1  
That's a good idea. But fortunately -- i don't have to use it :) So I noticed the guy in the launchpad site mentioned that the bug 'went away'. I turned off automatic updates so I guessed this might be the difference. Viola! So for anyone who comes upon this site: if you're running ubuntu 12.04 -- just update and use the DISPLAY & GSETTINGS_BACKEND declarations to get gsettings working with anacron. Thanks MHC for your help. –  yoshi Jan 2 '13 at 15:42

Instead of using:

DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '"file://'$wallPath'"

try to use:

PID=$(pgrep gnome-session)  # instead of 'gnome-session' it can be also used 'noutilus' or 'compiz' or the name of a process of a graphical program about that you are sure that is running after you log in the X session
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
gsettings set org.gnome.desktop.background picture-uri '"file://'$wallPath'"

See the explanation in my answer here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.