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!
- Scrape the website
- Check if my submission date is already resolved
- Notify me
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!
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:
- Use quotes with the echo, so that the HTML doesn’t get squashed into a single line.
greplikes to work with separate lines.
-A 1grep option gives me the line with the searched string occurrence plus 1 line after it. For me, the availability is noted on the following HTML row.
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
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!" email@example.com < message.txt
Notes about the command above:
-soption marks the Subject field of the email
message.txtis your predefined file with the email message. You can write anything in it, really :)
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:
- Creating an empty check file
- Deleting the script itself (very destructive in my opinion)
- Remove the crontab job running the script
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
grepwill invert the search results. So all lines not running your script will be displayed.
- Upload the edited cron jobs under your username (
-at the end isn’t a typo, it takes the standard output of the last command as input.
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!