Linux Bash Script Weather Forecast Email Service

Background

This post is to demonstrate the simplest way to set up your personal Weather Forecast Email Service using basic Linux Bash Scripts and Cron job. You can put scripts on your VPS, your Rasberry PI, or any other form of Linux server.

Environment

  1. Operation System: Operating System: CentOS Linux 7 (Core)
  2. Kernel: Linux 3.10.
  3. Weather Forecast Source: Australian Bureau of Meteorology, please be aware of the Copyright Statement on BoM’s website, which I will list at the end of the article.

Create your download Script for the Weather Information

  • Create a file/bash script, let us call it monitor, if you are not familar with Vi, you can use nano or any other text editor instead

vi monitor

  • Put below text in, you can see it is very straightforward, download the weather forcast information, sleep for 5 seconds then exit.

#!/bin/bash

#Update the Weather

wget -nv –output-document=/monitor/IDW12300.txt ftp://ftp2.bom.gov.au/anon/gen/fwo/IDW12300.txt
sleep 5

exit

Note: you can find the Capital City list here:

  • Canberra = IDN10035
  • Melbourne = IDV10450
  • Brisbane = IDQ10095
  • Perth = IDW12300
  • Hobart = IDT13600

Create your email sending script

  • Similar to above, create a new script using your favourite text editor

vi weather

  • Put below content in

#!/bin/bash

#ICT Fella’s free weather forecast service

/bin/mailx -r [email protected] -s “ICT Fella Weather Forecast” [email protected] [email protected] < /monitor/IDW12300.txt

exit

  • Setup Cron job, for people familar with Linux, Cron is a handy “Task Scheduler” (if you are talking Windows) in Linux word

vi /etc/crontab

Add 2 lines in to run the scripts every day, download the weather forecast at 6:30AM, then send the forecast at 6:44:

30 6 * * * root /root/monitor
44 6 * * * root /root/weather

Conclusion

It is that simple, the reason I split into 2 scripts is I do have a bunch of other tasks running in them ( such as monitoring hardware usage, monitoring Currency exchange rate), separation out makes me have more leverage of each component. This dummy Email Service has been running for the past 8 years thanks to BoM’s awesome free FTP service.

An email will be something like below:

Useful links

BoM websites and Copyright Statement: http://www.bom.gov.au/catalogue/anon-ftp.shtml

SirLeech’s GitHub https://github.com/sirleech/weather_feed

Leave a Comment

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