Automation Using Shell Script

Automation Using Shell Script

Table of Contents
    Add a header to begin generating the table of contents

    Introduction

    In today’s world, we are talking about programs or applications getting more intelligent – Self driving cars, M2M IoT communication, Virtual assistant applications like Google bots etc.

    At the crux of the intelligence lies automation, where applications are doing some of the mundane activities done by a human being with minimal or no human interaction. Automation results in efficient usage of resources – Time, Human resources, Cost etc.

    As such the topic automation is a very huge and a topic of study by itself. In this blog post, we are going to consider Linux shell scripting as an example and discuss few examples of automation using shell script.

    As discussed in the previous blog, shell script is an interpreted language. When a program or set of instructions is written in any language to perform a specific task, it has to be either compiled or interpreted.

    When a code is compiled using compiler (ex: C programming), language used to write code is said to be compiled language and if code is interpreted line by line using interpreter, the language (ex: Shell scripting) used in writing instructions is said to be interpreted language.

    A Brief Intro to Linux Shell Script :

    Shell script is a computer program run by Linux shell. It is a command line interpreter. Operations performed by shell scripts are file manipulation, program execution and printing text. There are various types of shells available for user. They are:

    • The Bourne shell (sh)
    • The Bourne Again Shell (bash)
    • The C shell (csh and tcsh)
    • The korn shell (ksh)

    If an user wants to perform a task, it can be achieved by writing a script. And if the task has to be repeated daily or in a specific condition / regular interval of time, it can be automated by some facilities provided by the Linux Shell.

     

    Linux Shell scripting is very powerful when it comes to command-line automation, as it combines programming constructs (ex: for loop) with linux commands (ex: du command for disk usage) that makes it a natural choice for automation.

    Cron:

    Linux automation relies on the powerful utility called “cron,” also known as a job scheduler. It automates tasks based on a set schedule known as “crontab.” Just like an alarm application on mobile phones.

     

    Crontab is where the schedule is configured, and cron executes the commands according to that schedule. Crontab creates a file containing instructions for the cron daemon, a long-running process responsible for executing the commands at specified intervals.

    • Schedule a job for specific time of the day
    • Schedule a job for every minute
    • Schedule a job for more than one time
    • Schedule a job for certain range of time and many more

    With Cron, task automation is possible by editing the crontab file using the ‘crontab -e’ command. Simply specify the task’s timeline and the application link to trigger automated tasks.

     

    For instance, the script/application named “file.sh” is set to execute daily at 5:15 PM. Refer to the crontab man page for additional options and details.

    Automation using Shell Script
    Fig 1: Code Execution

    Now that crontab understanding and configuration is done, appropriate examples for automation need to be discussed. In this blog post, three examples are taken to demonstrate the automation use-case.

    Example 1: Birthday Reminder

    Sending automated birthday wishes with personalized message can be one simple use-case to be considered. In order to do this task automatically, instead of individuals remembering them, a Comma Separated File (CSV file) to be created with Name, Email and Date of Birth information. An example CSV file can be as follows:

    Automation using Shell Script
    Fig 2: Example

    A Shell script can be written to read all the information from CSV file and compare their month and date information with the system’s current date and month. In case of a match, it indicates that particular individual’s birthday to whom an email can be send with a personalized message. The script code is provided below.

    Code:

    Assume the name of the script file as birthday.sh, which can be configured into the crontab in order to do automated check on a daily basis (say at 9:30 AM) and send the email in a personalized way.

    A sample crontab in that case would look as follows:

    Automation using Shell Script
    Fig 3: Example

    Example 2 : Preventive System Alerts and Maintenance

    Understanding system problems and taking preventive actions is one of the key requirement of a system administrator. For example, let us take a use-case of an organization with more than 1000 employees.

     

    One of the common system problem is about hard disk getting full, which may result in larger system related problems. What if the system administrator gets an automated alert if any of the employee’s hard disk capacity goes more than 90% occupation.

     

    Upon getting such alert, the system administrator can inspect the system and advise the employee by cleaning up / freeing up some space in the hard-disk. To estimate current disk usage df and du commands are used.

     

    Few examples of these command usage is given as follows. Combing this command with shell script and crontab, the pro-active alert system can be set up.

    Disk Usage

    Automation using Shell Script
    Fig 4 : Example

    Disk Free

    Automation using Shell Script
    Fig 5: Example

    Code:

    Automation using Shell Script
    Fig 6: Example

    Example 3 : Remote Device Management and Diagnostics

    To determine the available disk space on an external device like Raspberry Pi connected to our machine, we need to obtain its IP address first. Once the IP address is detected.

     

    We can use the script mentioned earlier to find the disk space. Automating this process is possible by placing the script in the crontab, as explained in the previous examples.

    Code:

    • As shown in the three examples above, shell script automation is versatile, catering to normal users (birthday reminders), system administrators (system alerts), and developers (IP address changes).
    • Open Source offers numerous options, like “vagrant” software, which automates the configuration of development environments, saving time in setting up machines individually.
    • The scope of automation is vast, covering various use-cases beyond the examples provided in this blog post.

    Conclusion

    • Linux shell offers various tools, including Cron daemon, empowering developers with automation capabilities.
    • In addition to scripting, tools like AutoIT, Selenium, Robotium, and others aid in automation. This saves time and allows for more productive endeavors instead of repetitive manual tasks.

       Happy Learning !!!!

    People Also Ask(PAA)

    • Sending automated birthday wishes with personalized messages based on a CSV file containing Name, Email, and Date of Birth information.
    • Implementing preventive system alerts and maintenance by checking hard disk usage and sending alerts if the capacity exceeds a certain threshold.
    • Cron, also known as a job scheduler, plays a vital role in automation by executing tasks automatically according to a set schedule.
    • It allows users to configure jobs at specific times, intervals, or on certain days, enabling the automation of repetitive tasks and system-level alerts.

    Share this material with your friend:

    Leave a Comment