[How To] Write a script which notifies and alerts you in Linux

Puneet Singh
techpsl
Published in
3 min readDec 14, 2013

Finally I got free from my End-Sem exams, phew! But not so fast, I have to manage many things in my one and half months of winter holidays and that too in this chilling weather of Buffalo.
One of the most important things on my priority list is to pay my next semester’s fees. Now here is the problem, it is really hard to track USD to INR rate. I had to go to XE or Yahoo Finance to know the current exchange rate. This gave me hard time, when the rate is changing continuously with high variability. So I decided to write a script and run it as a daemon or cron job which will notify me if the Dollar falls or rises from a particular threshold.
For this purpose, I have chosen Elementry OS/Ubuntu which is installed in my system (and BTW, my laptop can almost serve as a server, as I rarely switch in off. At best I would just make it go to sleep mode for a few hours). I would use Python and Perl for this purpose.
So let me show you how I did it. First, let’s make a script which would notify me on desktop if I am The rate falls below a threshold, we will call this a notifier script:


notify.py

#!/usr/bin/python
import sys
import pynotify

if __name__ == "__main__":
if not pynotify.init("icon-summary-body"):
sys.exit(1)

n = pynotify.Notification(
sys.argv[1],
sys.argv[2],
##dont remove the below line
"notification-message-im")
n.show()

This script would simply use the Ubuntu notification system and would notify you on top-right corner of your screen. (See screenshot)

Now we are ready to make a script which would continuously fetch the current exchange rate, but before that I also want that if I am not at my desk or not using the computer I must be notified through email. For that I would install, sendmail. It is really easy to install sendmail in Ubuntu, just fire this command:

sudo apt-get install sendmail

also create a filter in your mail box which would not send mails sent from your computer to spam folder, and test everything works properly by firing this command:

echo “Dollar Rate” | mail -s “Rate is at 61.67, just to inform you” your.mail@gmail.com

Now you must check your mail box, you would have received a mail like this:

I wrote a Perl script which uses Yahoo Finance API and keeps check of current exchange rate:

check_dollar.pl

use strict;
use warnings;
use LWP::UserAgent;
use LWP::Simple;
# my $url = "http://rate-exchange.appspot.com/currency?from=USD&to=INR";
my $url = "http://finance.yahoo.com/webservice/v1/symbols/INR=X/quote?view=basic";
my $oHTTPAgent = new LWP::UserAgent;
my $oRequest = HTTP::Request->new('GET');
my $i =0;
my $old_fh = select(STDOUT);
$| = 1;
my $rate;
select($old_fh);
while(1)
{
$oRequest->url($url);
my $sResponse = $oHTTPAgent->request($oRequest);
if ($sResponse->is_success) {
my $sPage = $sResponse->content;
($rate) = $sPage =~ m/price\">(\d+\.\d\d)/g;
$i++;
print $i."\t".$rate."\n";
if($rate<61)#set your threshold
{
my $xpl = `python /path/to/notify.py "Rate is" "$rate for 1 dollar"&>/dev/null`;
my $yoyo = `echo "Dollar Rate" | mail -s "Rate is at $rate, just to inform you" everlasting.puneet\@gmail.com`;
}

}
sleep(60);#set ping frequency, right now it would check in every 60 seconds
}

Now we are all set, all we need to do now is to start this process. If you can observe I have used an infinite while loop, if I want to make a cron job of this script I need to remove this loop.
Now either run the script as it is by inserting right path to “notify.py”, or if you want you can remove while loop and write cron job like this:
Run

crontab -e

This opens an editor. Therein you type :

*/1 * * * * perl /path/to/check_dollar.pl

We are all done, I hope you can be creative and add more type of notifications, and alerts.

--

--

Puneet Singh
techpsl

Machine Learning @ Factset, ex-blogger, wannabe-writer, Science-lover, student for life. Other interests: Entrepreneurship, Photography, Politics, SelfHelp