I'm trying to get a custom script to run, it does a basic job of switching randomly my wallpapers depending on the time of day from two folders, one folder has "day" wallpapers and the other has "night" wallpapers. The script is an executable already and it does work when I type the command directly into the terminal like this:

$ /path/to/my/scripts/twofoldersolution.py

So, I made a cron-job to invoke it every 5 mins (on my user crontab, not with sudo, it didn't work that way either), like this:

*/5 * * * * /path/to/my/scripts/twofoldersolution.py

Up until now, as far as I can tell, it should be working, and the logs reflect this with the following:

Aug 30 12:20:01 WarMachine CRON[2877]: (fawtytwo) CMD (/path/to/my/scripts/twofoldersolution.py)
Aug 30 12:25:01 WarMachine CRON[2937]: (fawtytwo) CMD (/path/to/my/scripts/twofoldersolution.py)
Aug 30 12:30:01 WarMachine CRON[3004]: (fawtytwo) CMD (/path/to/my/scripts/twofoldersolution.py)

My wallpaper should have changed 3 times already, but it stays the same, but I can still change it manually on the terminal.

Just to be on the safe side, here's my background changing script, it's a tiny bit hacky, but it works fine both on the terminal and running it with the IDE:

#!/usr/bin/env python3
import subprocess
import os
import sys
import time
from os import listdir
from random import choice

global path
path = {
    "day": "/path/to/my/Wallpapers/Day",
    "night": "/path/to/my/Wallpapers/Night"

def setwall(wall):
    #set the wallpaper
    command ="gsettings set org.gnome.desktop.background picture-uri "+\
    "'" + wall + "'"
    subprocess.Popen(["/bin/bash", "-c", command])

def convert_tosecs(t):
    # convert time of the day (hrs/mins) to seconds
    t = [int(n) for n in t.split(":")]
    return (3600*t[0])+(60*t[1])

def timeofday():
    #tells you if it's day or night
    t = convert_tosecs(time.strftime("%H:%M"))
    if t > 21600 and t < 75600: #6:00 - 21:00
        return ("day")
        return ("night")

def newwall():
    #chooses a new wallpaper depending on time of day
    wallpath = path[timeofday()]
    wallset = listdir(wallpath)
    return (wallpath + "/" + choice(wallset))

if __name__ == "__main__":
    wallpaper = newwall()

Any ideas on what might be wrong?


As this answer suggests, gsettings needs the DBUS_SESSION_BUS_ADDRESS to be set correctly. The following python function is based on that answer and the comments:

def setDbusSessionBusAddress():
    pid = subprocess.check_output(["/usr/bin/pgrep", "-u", os.environ["LOGNAME"] , "gnome-session"]).decode("utf-8").strip()
    environment = open("/proc/" + pid + "/environ")
    dbusSessionBusAddress = re.findall("DBUS_SESSION_BUS_ADDRESS=([^\x00]*)", environment.read())[0]
    os.environ["DBUS_SESSION_BUS_ADDRESS"] = dbusSessionBusAddress

Note that you need to additionally import re. Call this function before the gsettings call.

  • THAT'S IT!! Thank you very much! – fawtytwo Aug 31 '19 at 13:05
  • I'm glad I could help. Please consider accepting my answer if it helped you. – danzel Aug 31 '19 at 19:31
  • Didn't know how to do it haha, sorry, it's accepted now :D – fawtytwo Sep 1 '19 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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