Automating Email Notifications: Sending Scheduled Emails with Git Bash

Automating Email Notifications: Sending Scheduled Emails with Git Bash

In the realm of Linux system administration, mastering cronjobs is a crucial skill that empowers administrators to automate tasks and streamline operations. In this article, we'll embark on a journey to automate email notifications using a custom Bash script executed via cronjobs. Through step-by-step exploration, we'll delve into the intricacies of crafting the script and orchestrating scheduled email dispatches.

Setting the Stage: Understanding the Script

Our journey begins with a Bash script aptly named script.sh. Let's dissect its components and understand its functionality:

#!/bin/bash

Set the recipient email address

recipient="mie6@gmail.com" cc_recipient1="ga.oyi@google.ng" cc_recipient2="its06@gmail.com"

Function to send an email

send_email() { echo -e "To: $recipient Subject: LINUX TEST EMAIL CC: $cc_recipient1, $cc_recipient2

Dear Team,

Please see my email as my dedication to master cronjob with my scripts to enable me automate task" | sendmail -t }

Send an email every 2 minutes

while true; do send_email echo "Email sent. Sleeping for 2 minutes..." sleep 120 # 2 minutes in seconds done

Crafting the Solution: Email-Sending Script Explained

  1. Recipient Setup: The script begins by setting the recipient email address and CC recipients, enabling targeted communication with designated individuals or groups.

  2. Email Sending Function: A function named send_email is defined to encapsulate the email composition and dispatch logic. This function leverages the sendmail command-line utility to send emails programmatically.

  3. Scheduled Email Dispatch: Utilizing an infinite loop (while true), the script continuously invokes the send_email function at regular intervals. With a sleep duration of 2 minutes (sleep 120), emails are dispatched every 2 minutes as per the specified schedule.

Executing the Script: Automating Email Notifications

With our script ready, it's time to set the wheels in motion and automate email notifications:

  • Make the Script Executable: Ensure the script has executable permissions using chmod +x script.sh.

  • Execute the Script: Run the script in your terminal to initiate scheduled email dispatches: ./script.sh.

  • Monitor Email Dispatches: Observe as emails are sent every 2 minutes, delivering timely updates and notifications to recipients.

Conclusion: Empowering Automation with Cronjobs

In conclusion, our journey to automate email notifications using Linux exemplifies the power of cronjobs in orchestrating routine tasks and enhancing productivity. By crafting a custom Bash script and leveraging cron's scheduling capabilities, we've established a seamless system for delivering timely communications.

As you continue your exploration of Linux system administration and automation, mastering cronjobs opens doors to endless possibilities. Whether it's scheduling backups, performing maintenance tasks, or sending notifications, the ability to automate tasks empowers administrators to achieve more with less effort.

Stay tuned for more insights and explorations into the world of Linux system administration and automation. Through continuous learning and experimentation, let's unlock the full potential of Linux-based solutions and elevate our efficiency to new heights.

Author Bio: Gbenga Akinbajo is a Cloud/DevOps engineering student with a passion for Linux system administration, automation, and open-source technologies. With extensive experience in Technical writing, Gbenga strives to share knowledge and insights to empower others in their journey of mastering Linux. Connect with Gbenga on LinkedIn or visit https://hashnode.com/draft/65f5902fb58a1eb56ae54e10 to explore more Linux-related content and projects.