Vojtěch Struhár

Posted on July 1, 2022 | #bash , #storytime

thumbnail

Automate tasks with a simple Bash script

Recently, I finished my motorcycle driving course and I submitted a request for a new driving license. In the transport office, I was told that my license will be ready in about 15 days and I should monitor their website to know when can I pick it up.

There is a list of submission dates on the website and whether or not requests from that day are ready to be picked up. But who’s got time to check their website every day? Let’s automate this!


Battle plan:

  1. Scrape the website
  2. Check if my submission date is already resolved
  3. Notify me
  4. Cleanup

Should be easy enough!


Scrape the website

There are many fancy tools for scraping websites. But for our needs, just a very simple command using curl will do!

website=$(curl $LICENSE_URL)

We now have a raw HTML website code inside the website bash variable.

Check the date of my submission

I submitted my request on 28.6.2022. We can use the grep utility for getting the exact HTML content we need. You will have to tailor your script to the specific format you are looking for, but generally, it could look something like this:

echo "$website" | grep -A 1 "on 28. 06. 2022" | grep "available"

Notes about the line above:

If the check fails, you can exit the script right now with || exit 1 at the end of the line.

Let me know!

As for notification options, it is actually very easy to send emails right from the Linux command line. The email protocol has been around for a very long time, it’s open and decentralized, which is perfect.

You can install mail utility by running sudo apt-get install -y mailutils .

Then you can send an email right from the script by running something like this:

mail -s "Driver license is ready!" youremail@gmail.com < message.txt

Notes about the command above:

Cleanup

This script will reside on my Raspberry and it will be run by a crontab job. But once it finds success for the first time, it is not needed anymore. Effectively we only need it to run once.

There are multiple ways to secure that a script runs only once:

We will focus on the last option.

Crontab job setup

I will set up a new crontab job with crontab -e and configure it to run every day every working hour. That is when the website could update I think. The rule is this:

10 6-16 * * * ./my_script.sh

The job will run every hour between 6 AM and 4 PM at the 10th minute. Like 6:10, 7:10, 8:10, etc…

Crontab job cleanup

You can remove the job right from the script with this line:

crontab -l | grep -v "./my_script.sh" | crontab -u $(whoami) -

Notes about the command above

Sit back and relax!

Congratulations! You just automated one more thing in your life. Now instead of checking a website every morning, you can wait for an email to arrive on your phone.

Was it worth your time? Probably not.

Was it worth the fun? Absolutely!

Read next → Astro 404 page with Apache