Automatically set your desktop background based on the weather at your current location (Ubuntu)

Sam Kavanagh

28 May 2019

This one’s a bit more involved than my usual blog posts, so I made it a tutorial… because I can.



[sam@samantha] λ curl --silent"%C"
Light Rain

Setup & Code

1. Create weather-wallpapers/ folder

Example Weather Wallpapers

2. Save the weather script somewhere


# Get the DBUS_SESSION_BUS_ADDRESS environment variable which gsettings needs but cron by default runs without
PID=$(pgrep gnome-session | tail -n1)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

 # Get the current weather in your location
CUR_WEATHER=$(curl --silent"%C")

# Look in a folder 'weather-wallpapers' for an image with a filename corresponding to the weather condition
# and sets that as the desktop background. NOTE: The path below will need to be updated to be edited to point to your folder.
gsettings set org.gnome.desktop.background picture-uri "file:///home/sam/Pictures/weather-wallpapers/${CUR_WEATHER}.jpg"

3. Add the script as a cron job

*/5 * * * * /home/sam/.rc/weather # Adjust the '5' to however often you want to query for weather updates.

Done! 😎