Rocket Squirrel Rocket Squirrel
Rocket Squirrel

A global community of coders, developers, and designers

June 2024


Adding SMS Notifications to Your Bash Scripts

Dan DulaneyDan Dulaney

Hello! Today, we’re going to add text message notifications to our bash scripts. To do so, we’re going to use the Amazon Web Service’s SNS SMS api, via AWS CLI. For this tutorial, you will need to have an Amazon Web Services account. Signing up for an AWS account is beyond the scope of this tutorial.

Cost Note: The first 100 text messages / month are free. After that, you may start incurring charges.

Step 1: Create an Appropriate AWS User

Start by creating a new User or Group in AWS with the appropriate privileges. You do NOT want to use your root account in production, for security reasons. I would suggest creating a group here, and adding your user to that group, so you can easily expand privileges  if you want to do more things via AWS CLI.

Step 2: Install and configure AWS CLI

There are many ways to install the AWS CLI. If your platform supports it, I strongly suggest installing with PIP: sudo pip install awscli

If you cannot or wish not to use PIP, see the official guide here for installing AWS CLI.

To configure, first enter: aws configure This will prompt you for the following, which you should have noted from the step above.

AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: ENTER

The access key and secret key should have been generated in step 1. The region can be any of the AWS regions, but if you choose one that does not support SMS, you will have to force that region in the command in step 3.

Step 3: Test It Out!

Try the following code in terminal, to test and make sure everything works. Be sure to substitute your phone number with the placeholder.

aws sns publish --phone-number=1-555-555-5555 --message "Your Message Here"

If your default region does not support SMS, you can add this tag to force it to one that does: --region us-west-2 .

--message can also take a file name as an argument, with --message file://file.txt, instead of a string, and send the contents of that file.

Wrapping Up

That’s it! Feel free to add the line above to any of your bash scripts or cron jobs, to alert you when something occurs. For example, I have a weekly backup in addition to my nightly, so I have it text me when that has been completed.

Do you have any novel uses? I’d love to hear about them below!

Math teacher by day, freelance WordPress developer by night through Codeable and word of mouth. I specialize in 3rd party API integrations, WordPress optimization (server and site both), and custom plugin development.

Comments 0
There are currently no comments.