Reputation: 65
I'm trying to get a simple python script to execute periodically on arch linux using cron, but when I run it in cron it behaves differently then why I run it in the terminal.
The script downloads a wallpaper from NASA's picture of the day and makes it my wallpaper using the "feh" program. When I run the script in the terminal, everything is fine, but when I run it in cron, it downloads the file but doesn't make it the wallpaper.
Any help would be appreciated.
#!/usr/bin/env python
import time
import os
import requests
import tempfile
from bs4 import BeautifulSoup
WALLPAPER_LOCATION = "/home/user/.wallpaper.jpg"
def main():
if os.path.exists(WALLPAPER_LOCATION):
os.remove(WALLPAPER_LOCATION)
website = "https://apod.nasa.gov/apod/astropix.html"
html_txt = getHTML(website)
img_url = get_image_URL(html_txt)
downloadFile(img_url, WALLPAPER_LOCATION)
os.system("/usr/bin/feh --bg-scale " + WALLPAPER_LOCATION)
def downloadFile(url, filepath):
with open(filepath, 'wb') as handle:
response = requests.get(url, stream=True)
if not response.ok:
print(response)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
def getHTML(url):
html_txt = ""
temp_html_file = tempfile.NamedTemporaryFile()
downloadFile(url, temp_html_file.name)
with open(temp_html_file.name, "r") as reader:
html_txt = reader.read()
return html_txt
def get_image_URL(html_doc):
base_url = "https://apod.nasa.gov/apod/"
soup = BeautifulSoup(html_doc, "html.parser")
return base_url + soup.findAll('img')[0]["src"]
if __name__== "__main__":
main()
Upvotes: 1
Views: 108
Reputation: 65
@thatotherguy was right. The problem was that cron didn't know what display to use so that feh could set the wallpaper. The problem with X11 programs running in cron is mentioned in a link that @thatotherguy provided, and is also found in the arch wiki.
I added env DISPLAY=:0
to the start of my cron job and everything is working fine now.
Upvotes: 1