I've made a simple python script to change my wallpaper (Ubuntu 18.04) by randomly taking an image in a specific folder. I use gsettings command. It works when I run it manually, but I want to automate it with crontab.

I had the following error because crontab hasn't all variables I had when I run my script manually:

(process:xxxxx): dconf-WARNING **: 15:07:01.547: failed to commit changes to dconf: Impossible de lancer automatiquement D-Bus sans $DISPLAY X11

So I found some solutions like :

dbus_session_bus_address = 'PID=$(pgrep gnome-session | head -n1) && export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-) && '

command = dbus_session_bus_address + 'gsettings set org.gnome.desktop.background picture-uri ' + getRandomImage()

It seems work for most people but I have a problem, my crontab hasn't permissions :

grep: /proc/$pid/environ: Permission denied
dconf-WARNING **: 15:19:01.514: failed to commit changes to dconf: The address indicated is empty 

So I run the script with sudo crontab -e, but I have the following error :

dconf-WARNING **: 15:22:01.807: failed to commit changes to dconf: Connection is closed

Also, I prefer use my user crontab.. What is the best way to do that ?

  • Are you actually using your user crontab (crontab -e) or root's (sudo crontab -e)? I wouldn't be surprised if the latter resulted in a permission error, since /proc/$pid/environ should only be readable by $pid's owner – steeldriver Jul 7 '18 at 15:31

Thanks to steeldriver's comment, I found the solution !

Actually, the pgrep gnome-session command gives me 2 PID.
When I ls -l /proc/, I can see the first pid directory belongs to gdm (Gnome Display Manager), and the second belongs to my user.

So in my script, I just replace PID=$(pgrep gnome-session | head -n1) by PID=$(pgrep gnome-session | tail -n1) to use the PID associate to my user.

I don't know if this solution (with tail -n1) can work for everyone. I think the best thing to do is to run the pgrep gnome-session command, look at the owner of pid folders, and choose the one that belongs to our user.

Hoping that it will be useful !

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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