Gsettings with Cron

Weird behavior of gsettings set a new desktop.background with cron

In addition to $DISPLAY you may need to set $DBUS_SESSION_BUS_ADDRESS environment, because gsettings is using dbus (discovered when not setting DISPLAY, the error message talks about being unable to auto-launch D-Bus); you might try:

DISPLAY=:0
*/5 * * * * eval $(ps -ww -p $(pgrep gnome-session) -o cmd= e | fmt -1 | grep DBUS_SESSION_BUS_ADDRESS) python3 /usr/local/cronwal/cronwal.py

This gets the session address from the environment of the gnome-session process owned by the crontab invoker, and sets it in the environment of your python3 process, allowing the forked gsettings command to communicate over the session bus.

You can check if DBUS_SESSION_BUS_ADDRESS is set in your terminal with printenv and copy that if there's some syntax error above (no way for me to test), but it will not work on the next gnome session of course if you hardcode it like that, you'd have to change it every time:

DISPLAY=:0
*/5 * * * * DBUS_SESSION_BUS_ADDRESS="whatever printenv says" python3 /usr/local/cronwal/cronwal.py

Setting background with Python2.7 Crontab in Ubuntu 12.04

It seems its a problem with running gsettings under cron. Changing the os.system command to include DISPLAY=:0 GSETTINGS_BACKEND=dconf does the trick.

os.system("DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '%s'" % (setup))

How do I set gsettings during startup?

It is likely the script runs when still there is no session available.

Given it is a live CD and you have control of it, you might want to change the defaults values in the schema. For vino, you should change the default values in /usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml.

Unable to change wallpaper using cronjob on ubuntu

I tried all the solutions but none helped. The problem was, I was still unable to access the environment variables(DBUS_SESSION_BUS_ADDRESS). So one could check if those are available first.

What worked for me is simply running the script in current shell environment. This can be done by running the script with a . as in cronjob file -

* * * * * . /path/to/script/Wallpaper_Changer.sh

More information : Global environment variables in a shell script Understanding Unix shells and environment variables

Python os calls don't work in crontab

The problem is that gsettings needs to know what screen to run on. If you run it in a terminal, it will run on the screen the terminal is on, but if you run it in crontab, it doesn't have any screen in its context.

You might be able to do this by adding a switch to the gsettings command, or an environment variable, to specify the display. It depends on whether gsettings was ever written to work in a situation where the display couldn't be inferred.

Set gsettings in Vagrant provision script

While the accepted answer to the DBUS Session question didn't work for me, the linked answer under it did, Run DBUS.

dbus-launch gsettings set ...


Related Topics



Leave a reply



Submit