Setting wallpaper from cron
Setting wallpaper from cron requires setting gsettings
. Since cron
runs with a very limited set of environment variables, you will need set a special variable, called:
DBUS_SESSION_BUS_ADDRESS
Not the (what you would expect) DISPLAY
-variable.
See also here how to do that.
Alternatively
Alternatively, you can use the simple script below. On startup, the script sets the corresponding wallpaper, then all the script does is wait until midnight, to change the wallpaper. Then again sleep until the next midnight and so on.
The script
import time
import os
import subprocess
picsdir = "/home/jacob/Bureaublad/pics"
images = sorted([os.path.join(picsdir, pic) for pic in os.listdir(picsdir)])
def calc_sleep():
secdata = time.strftime("%H %M %S").split()
seconds = (int(secdata[0])*3600)+(int(secdata[1])*60)+(int(secdata[2]))
# calculate the sleep time until next midnight
return 86400+1-seconds
while True:
# weekday
day = int(time.strftime("%w"))
# the corresponding image from the set folder
image = images[day-1]
# set the image from gsettings
command = ["gsettings", "set", "org.gnome.desktop.background",
"picture-uri", "file://"+image]
subprocess.check_call(command)
# calculate the time to sleep until next midnight
wait = calc_sleep()
time.sleep(wait)
How to use
- Create a directory with seven wallpapers
- Copy the script into an empty file, save it as
wallswitch.py
- In the head of the script, set the path to the wallpapers
Test-run the script:
python3 /path/to/wallswitch.py
The wallpaper, corresponding to the day of the week should be set.
If all works fine, add it to Startup Applications: Dash > Startup Applications > Add. Add the command:
/bin/bash -c "sleep 10 && python3 /path/to/wallswitch.py"