Skip to main content

Command Palette

Search for a command to run...

Real Time Projects In Shell Script

Published
4 min read
B

I am DevOps Engineer who works on DevOps tools like Docker, Kubernetes, Terraform, Git, GitHub, Jenkins and AWS services.

Monitoring The Free RAM Space In a Linux Server

#!/bin/bash

FREE_MEM=$(free -mt | grep Total | awk '{print $4}')
TH=100

if [[ $FREE_MEM -lt $TH ]]
then
        echo "Memory Warning"
else
        echo "Free Memory is $FREE_MEM M"
fi

Output -

-> Here free -mt command will give the total memory, used memory and free memory

-> Using | symbol, we can send the output of free -mt command to grep command

-> Now the output of free -mt | grep Total is -

-> Now output of free -mt | grep Total | awk '{print $4}' is -

1. Monitoring free DISK space and sending an Alert Email

#!/bin/bash

USAGE_DISK=$(df -h | egrep -v "tmpfs|Filesystem" | grep /dev/root | awk '{print $5}' | tr -d %)

TH=20
TO="bhaskarmehta422@gmail.com"
if [[ $USAGE_DISK -gt $TH ]]
then
        echo "Disk usage is high - $USAGE_DISK %" | mail -s "Disk Space Alert" $TO
else
        echo "DISK space is sufficient - $USAGE_DISK"
fi

Output -

Note - Before running the above script you have to set up postfix on the Ubuntu server.

Please check this link to Setup - https://bhaskar422.hashnode.dev/setting-the-postfix-mail-in-ubuntu

2. If the file size is large than a given size then archive that large file

Project Requirement -

-> In a given directory, if you find files more than a given size ex: 3MB then Compress those files and move them to an archive folder

#!/bin/bash

# Variables
BASE=/home/ubuntu/
DEPTH=1


# Check if the directory is present on not
if [[ ! -d $BASE ]]
then
        echo "Directory does not exist - $BASE"
        exit 1
fi

# Create an archive folder if it does not exist 

if [[ ! -d $BASE/archive ]]
then
        mkdir $BASE/archive
fi

# Find the list of files larger than 2 MB

for i in `find $BASE -maxdepth $DEPTH -type f -size +3M`
do
    gzip $i || exit 1
    mv $i.gz $BASE/archive || exit 1
done

-> To automate this script, create a cronjob-

Type crontab -e

00 00 * * * cd <path of your script directory> && bash <Script  name> # Example 00 00 * * * cd /home/ubuntu && bash archive_project.sh

-> It means every day at 12 AM this script will run.

3.Create a User In Linux Using Shell Script

Requirement -

-> Script should be executed with root user else exit with status 1 and error message

-> Script will take 1st argument as a user and rest will be treated as comment

-> Auto-generate password for the user

-> Upon successful execution of script, display the following

1. username : <username>

2. password : <auto-generated password>

3. host : <host name>

#!/bin/bash

# Check user is logged in a as root user
if [[ $UID -ne 0 ]]
then
        echo "User is not logged in as a root"
        exit 1
fi

# Check if the User is providing atleast one argument

if [[ $# -lt 1 ]]
then
        echo "Please enter at least on eargument like - $0 User_Name [Comment] "
        exit 1
fi

# Store the first argument in username
username=$1

# Shift the remaining argument as a Comment
shift
comment=$@

# Create passsword
password=$(date +%s%N)


# Create user
useradd -c "$comment" -m $username

# Check if the user is successfully created or not
if [[ $? -ne 0 ]]
then
        echo "User not created successfully"
        exit 1
fi

# Set the Password for the user
echo "$username:$password" | chpasswd

# Check if password is successfullt set or not
if [[ $? -ne 0 ]]
then
        echo "Password could not  be set up"
        exit 1
fi

# Force password change on first login
passwd -e $username # -e means expire when user does first login

# Display the username, password and hostname where it is created

echo "Username : $username" 
echo "Password : $password"
echo "Hostname : $(hostname)"