# #TWSBashBlazeChallenge - Day 4

Welcome to the Bash Scripting Challenge - Day 4! This challenge is designed to test our Bash scripting skills and problem-solving abilities in the context of process monitoring and management.

## **Scenario 1:** Monitoring a specific process

We have to write a Bash script that efficiently monitors a specific process on a Linux system. The script's primary goal is to ensure the process is always running, and if it unexpectedly stops, it should be restarted automatically.

**Tasks -**

### 1: Process Selection:

The script should accept a command-line argument to specify the target process to monitor. For example: `./monitor_process.sh <process_name>`.

Let's use the "nginx" process for this task. So to install nginx in your Linux system, follow these steps -

`sudo apt update`

`sudo apt install nginx`

`sudo ufw app list`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691041687249/9124b05d-a568-4326-b438-e5d355536462.png align="center")

`sudo ufw allow "Nginx HTTP"`

`sudo ufw enable`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691041745193/6e05cf6c-514f-41c8-b500-79d90f0e291e.png align="center")

`sudo ufw status`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691041762703/011621d6-5e09-4942-b5a5-1d1c82f21e74.png align="center")

`systemctl status nginx.service`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691041781377/bb3c7019-20f6-4a05-a2d4-0df744da757d.png align="center")

Now, we have nginx service active in our system, So let's proceed with the task.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691071003703/bb688ba3-d0f0-4b47-8924-67c7b798d004.png align="center")

We will tell user to enter a service name if he hasn't entered anything along with the script name.

### 2: Process Existence Check:

* Implement a function that checks if the specified process is currently running on the system.
    
* If the process is running, print a message indicating its presence.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691071101332/03e94706-1838-42ae-a984-76060fc725f6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691071331267/9d16bc68-3d0e-438a-8a25-17754cc58744.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691071351194/0f2b1a12-e3ee-443b-bed2-83ec01f690f5.png align="center")

### 3: Restarting the Process:

* If the process is not running, implement a function that attempts to restart the process automatically.
    
* Print a message indicating the attempt to restart the process.
    
* Ensure the script does not enter an infinite loop while restarting the process. Limit the number of restart attempts.
    
    Let's stop the process manually -
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691071208214/c70e1f95-c278-4aca-b295-8e06f0355598.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691071299413/f0779e6a-1e75-4dfb-8d27-b128b414f93a.png align="center")
    

### 4: Documentation:

* Include clear and concise comments in the script to explain its logic and functionality.
    
* Write a separate document describing the purpose of the script, how to use it, and any specific considerations.
    

## **Scenario 2:** Monitoring Metrics Script with Sleep Mechanism

This project aims to create a Bash script that monitors system metrics like CPU usage, memory usage, and disk space usage. The script will provide a user-friendly interface, allow continuous monitoring with a specified sleep interval, and extend its capabilities to monitor specific services like Nginx.

### 1: Implementing Basic Metrics Monitoring

Write a Bash script that monitors the CPU usage, memory usage, and disk space usage of the system. The script should display these metrics in a clear and organized manner, allowing users to interpret the data easily. The script should use the top, free, and df commands to fetch the metrics.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691071583973/96af15b9-2c58-4529-a592-6d9af3874e27.png align="center")

### 2: User-Friendly Interface

Enhance the script by providing a user-friendly interface that allows users to interact with the script through the terminal. Display a simple menu with options to view the metrics and an option to exit the script.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691071607696/481eb58f-2fb7-4d2d-8782-36403b4958aa.png align="center")

### 3: Continuous Monitoring with Sleep

Introduce a loop in the script to allow continuous monitoring until the user chooses to exit. After displaying the metrics, add a "sleep" mechanism to pause the monitoring for a specified interval before displaying the metrics again. Allow the user to specify the sleep interval.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691072774429/675ecadc-8a7b-46f9-8ece-8a263002a00b.png align="center")

### 4: Monitoring a Specific Service (e.g., Nginx)

Extend the script to monitor a specific service like Nginx. Check if the service is running and display its status. If it is not running, provide an option for the user to start the service. Use the systemctl or appropriate command to check and control the service.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691072877641/2ccffc2a-9b5d-4761-97f9-239d818b0120.png align="center")

Now, let's stop it manually-

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691072912315/e1f9aa3d-5569-42cd-8d14-3d745220aa64.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691072981404/2cc3cfe6-d1fb-4b70-9800-9d2c184de118.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691073008023/b38ed09f-1658-489a-bad8-2306af6a9324.png align="center")

### 5: Allow User to Choose a Different Service

Modify the script to give the user the option to monitor a different service of their choice. Prompt the user to enter the name of the service they want to monitor, and display its status accordingly.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691073024576/c553daa3-142a-418a-9e91-2f7995abe120.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691073041604/a38e22f6-5de9-4a00-b8b0-2861731b011d.png align="center")

### 6: Error Handling

Implement error handling in the script to handle scenarios where commands fail or inputs are invalid. Display meaningful error messages to guide users on what went wrong and how to fix it.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691073082072/32949cb6-d017-456a-b062-3c8bb341bfbd.png align="center")

### 7: Documentation

Add comments within the script to explain the purpose of each function, variable, and section of the code. Provide a clear and concise README file explaining how to use the script, the available options, and the purpose of the script.
