Having Fun with Shell Scripting : Send Birthday SMS Alerts Via a Shell Script

In my previous article we had fun with Shell Scripts by automating birthday alerts via email. Using the same logic and a bit of tweaking we can create SMS alerts to be send to a user on their birthday.

First thing you would need is an SMS gateway and an account of your own to send SMS using REST API (curl). Most of the modern SMS gateways support sending SMS via API’s specially REST. I normally use Twilio since the company i work for we develop SAAS based SMS and IVR applications and Twilio is one of the best, cheapest and easiest platforms to rapidly build those kind of Apps. But you can use any other SMS gateway as long as it supports calling a REST API via curl

 

1. Create your data file which stores the birthdays. Lets call it ‘data_mob

$ vim data_mob

And add the FirstName,LastName,Month,Day.MobileNumber to file ‘data_mob‘ in below format. The Month and Day field corresponds to the birthday of the user. For Example John Doe’s birthday is on the 5th of Jan every year

John,Doe,01,05,+619988776655
Jane,Plain,01,23,+617766554433
Robert,Zemekis,07,31,+616677889911
Rudolf,Victor,01,17,+612233445566
Ken,Lancaster,07,06,+614455667788

Important : Please note the Month is First and then Day so the format is <firstname,lastname,mm,dd,mobilenumber>

 

2. Create the shell script which will check the file ‘data_mob‘ for dates using an IF condition and send SMS to the recipient if the birthday falls on current date

$ vim /home/oracle/mob_birthday.sh
#!/bin/ksh
data_file="/home/oracle/data_mob"
today_month=`date +%m`
today_day=`date +%d`
today_date="$today_month,$today_day"
all_friends="$(grep -v '^#' $data_file | grep $today_date)"
for friend in $all_friends
   do
   mob_number=$(echo $friend | cut -d , -f 5)
   if [ -z $mob_number ]; then
      continue
   fi
   first_name=$(echo $friend | cut -d , -f 1)
   last_name=$(echo $friend | cut -d , -f 2)
   full_name="$first_name $last_name"

/bin/curl 'https://api.twilio.com/2010-04-01/Accounts/ACxxxxxxxxxxx/Messages.json' -X POST --data-urlencode 'To='$mob_number'' --data-urlencode 'From=EASYORADBA' --data-urlencode 'Body=Happy Birthday '$first_name' :)' -u ACxxxxxxxxxxx:xxxxxxxxx

done
exit 0

The SMS gateway part of the code is the below line. Now this could differ if your using another SMS gateway, so you have to customize according to the format of your SMS gateways REST API structure

/bin/curl 'https://api.twilio.com/2010-04-01/Accounts/ACxxxxxxxxxxx/Messages.json' -X POST --data-urlencode 'To='$mob_number'' --data-urlencode 'From=EASYORADBA' --data-urlencode 'Body=Happy Birthday '$first_name' :)' -u ACxxxxxxxxxxx:xxxxxxxxx

Variables $mob_number and $first_name are used to  pick the mobile number and first name for the person from the data file ‘data_mob‘. So if your using another SMS gateway make sure you enter the variables $mob_number  where the TO field for mobile number of recipient is and in the body of SMS use variable $first_name to pick the name of the recipient and dispatch the SMS.

 

3. Create a cron job on the server to check the data file ‘data_mob‘ daily and send SMS

00 17 * * * /home/oracle/mob_birthday.sh

 

Sample of SMS dispatched for when John Doe’s birthday falls on current day

 

 

Even though it seems like a fun thing to do but the logic behind this shell script can be used to do some serious stuff. For example it can be used to probe the server/application for any critical faults and send SMS to System Admin if that event occurs

A few examples of which i use for shell scripts to send SMS alerts :

1. Probe API gateways to check connectivity and send an SMS if the API gateway goes down

2. Check disk space of critical servers and send an SMS if it exceeds a certsain threshold

3. Check Application log file for a particular event code and send SMS of the event code is occurring to frequently

4. Check critical database processes and send sms if one of them goes down

5. Run a data loading job and if the job fails send an SMS notification to the Database admin

 

And their are a million different scenarios you can apply this logic to. Be creative, let your grey cells run loose and keep shell scripting 🙂

Category: AIXBashLinuxScriptShell Scripts

Tags:

Leave a Reply

Article by: Shadab Mohammad