Weather Specific Backgrounds on Ubuntu with Apache, PHP, Cron and Weather.com

It has long been a goal of mine to create a system for my laptop which allows my background to mimic the weather where I live (Durham, NC). When it is raining outside, I want a background of rain. When it is sunny, the sun.

So, here is a basic step-by-step guide I used to cobble this together.

1. Install Apache on Ubuntu.

* JTirrell below makes a good point for those who don’t already need Apache: “Why install apache at all? php5-cli and just run the php file like a script.”

Open up a terminal and type in the following, being sure to use your password after entering in the command. Afterwards, you should now be able to open up http://localhost/ in your browser.

sudo apt-get install apache2

2. Install PHP
Open up a terminal and type in the following, being sure to use your password after entering in the command. You will now have PHP installed with your Apache build.

sudo apt-get install php5 libapache2-mod-php5

3. Create Directory /var/www/backgrounds
This is the directory which will store your backgrounds and the code necessary to generate the background.

4. CHMOD 666 Directory /var/www/backgrounds
This is necessary so that your system can write the background.jpg file whenever the weather status updates.

5. Create the file /var/www/backgrounds/maker.php with the following code
This is the PHP file that grabs the current weather based on your zip code, grabs the relevant background image and overwrites background.jpg with it.

6. Switch your Zip Code with Yours and Save
Make sure you switch the zip code unless you want your background to only mimic the weather in Durham, NC 🙂

7. Create Backgrounds to Match The Common Weather.com Statuses
…and Save Them to the /var/www/backgrounds folder. Make sure that you…

  • Resize them to your resolution
  • Save them with the Exact, Case Sensitive name of the status (like “Partly Cloudy.jpg”)

Some examples are below.

8. Set Background to /var/www/background.jpg in Ubuntu
This is the background image that gets regenerated every 5 minutes based on the status at Weather.com for your zip code.

9. Set Cron Job to Run maker.php Every 5 Minutes

5 * * * * wget -rvc http://localhost/backgrounds/maker.php > /dev/null

Thats it! Whenever you have an internet connection and your Cron job is running, you will have a background that mimics the current weather!

4 Comments

  1. Jtirrell
    Jul 8, 2009

    Why install apache at all? php5-cli and just run the php file like a script.

    I already run apache myself, but you make a good point if you don’t need apache anyway.
  2. ojiisan
    Jul 8, 2009

    Here’s an alternate method using Python…if that’s your flavor.

    import urllib

    imgsrc = ”
    zip = 27713
    storageLoc = ‘/Users//bg/current.gif’

    response = urllib.urlopen(‘http://www.wund.com/cgi-bin/findweather/getForecast?query=%i’%zip)
    msg = response.read().split(‘\n’)

    for n,line in enumerate(msg):
    if ‘Current Conditions’ in line:
    line = msg[n+1]
    # ‘<img src=”…”‘
    imgsrc = line.split(‘”‘)[3]
    break

    urllib.urlretrieve(imgsrc, storageLoc)

    Here I have it d/l the little current weather icon to a directory that the nice OS X program GeekTool watches and puts on my desktop. But it could easily be altered to check the image name or something and use that to drive the desktop background image change.

  3. ojiisan
    Jul 8, 2009

    for proper formatting of that code see:

    http://codepad.org/Ns89mwns

  4. or
    Jul 9, 2009

    what are the possible statuses? Partly Cloudy, Sunny, Rainy, snowy, clear, fog?

Submit a Comment

Your email address will not be published. Required fields are marked *