I wrote a python script to autogenerate a background pattern for my computer. I want to use cron to run it every 15 minutes or so. Here's the code:
#!/usr/bin/python
import os
import numpy as np
import svgwrite as svg
WIDTH = 1600
HEIGHT = 900
def circle_layer(dwg, size_range, blur, number):
blur_filter = dwg.defs.add(dwg.filter())
blur_filter.feGaussianBlur(in_='SourceGraphic', stdDeviation=blur)
g_f = dwg.add(dwg.g(filter=blur_filter.get_funciri()))
for c in xrange(0, number):
(x, y) = (np.random.random_integers(0,WIDTH), np.random.random_integers(0,HEIGHT))
rad = np.random.random_integers(size_range[0], size_range[1])
g_f.add(dwg.circle(center=(str(x),str(y)), r=str(rad),stroke="rgb(100,100,100)", fill="rgb(100,0,0)"))
return dwg
if __name__ == "__main__":
svg_doc = svg.Drawing(filename = "current_wallpaper.svg", size = (str(WIDTH), str(HEIGHT)))
svg_doc.add(svg_doc.rect(insert=(0,0), size=(str(WIDTH), str(HEIGHT)), fill="rgb(0,0,0)"))
svg_doc = circle_layer(svg_doc, (10,50), 20, 100)
svg_doc = circle_layer(svg_doc, (10, 20), 4, 100)
svg_doc.save()
import subprocess
proc = subprocess.Popen(["pgrep gnome-session"], stdout=subprocess.PIPE, shell=True)
(pid, err) = proc.communicate()
pid = pid.rstrip('\t\r\n\0')
print repr(pid)
cmd = "grep -z DBUS_SESSION_BUS_ADDRESS /proc/"+pid+"/environ|cut -d= -f2-"
proc = subprocess.Popen([cmd], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
out = out.rstrip('\t\r\n\0')
print repr(out)
cmd = "export DBUS_SESSION_BUS_ADDRESS=$"+out
print repr(cmd)
os.system(cmd)
os.system("/usr/bin/gsettings set org.gnome.desktop.background picture-uri file:///home/jofo/Pictures/Wallpapers/current_wallpaper.svg")
print "I ran!"
Basically, it uses svgwrite to draw some circles and add a gaussian blur. Then, I make a system call to gsettings to update the ubuntu wallpaper.
This works fine when I run it as ./wallpaper.py, but when I schedule it in crontab, the script runs, and the background pattern doesn't change.
Here's what I have in my crontab file doohickey:
MAILTO=""
*/1 * * * * /home/jofo/Pictures/Wallpapers/./wallpaper.py >> /home/jofo/Pictures/Wallpapers/cron.log
I know the script is running once a minute because it appends "I ran!" to the cron.log file once per minute. Unfortunately, the wallpaper never changes. If anyone knows why this might be, that would be great!
export
in a sub-process doesn't do what you hope it does. Tryos.environ["DBUS_SESSION_BUS_ADDRESS"] = out
instead. If you still need help, please consult [SO], because the rest is a programming question. P.S.: Spawning sub-processes to find processes or read files isn't very Pythonic since all that can be done from within Python with the right modules. Ther's even one for gsettings/dconf. – David Foerster Aug 14 '16 at 08:54os.environ["DBUS_SESSION_BUS_ADDRESS"] = out
– J. Ford Aug 15 '16 at 19:40os.system("/usr/bin/gsettings set org.gnome.desktop.background picture-uri file:///home/jofo/Pictures/Wallpapers/current_wallpaper.svg")
os.system("echo $DBUS_SESSION_BUS_ADDRESS")