Day 5 - Advanced Linux Shell Scripting with User management

Overview of the CRON format, a commonly adopted syntax used to specify ...

What is Cron?

Cron is a tool in Linux that helps schedule and automate tasks. It's a job-scheduler for Unix systems, following instructions to schedule and perform tasks.

For example, if we want to automatically track and back up logs at 3 am instead of doing it ourselves, we can use a "Cron Job" to handle it for us.

What is a Cron Job?

A Cron Job is a set of instructions or commands in Linux used to schedule tasks. It lets users automate and carry out repetitive tasks at specified times, dates, or frequencies, executing specific commands, scripts, or programs.

Commonly employed for tasks like log tracking, system maintenance, and routine operations, Cron Jobs enable automation without the need for manual intervention.

Note: The cron daemon is responsible for managing Cron Jobs.

What is a crontab?

A crontab is a configuration file that stores instructions for a cron job. The term "crontab" comes from "cron table". In this file, you'll find lines specifying when certain tasks should run and the commands to execute.

Note: Crontab files are typically stored in the /etc/crontab folder.

Understanding the Crontab Syntax

The Linux Crontab Format is represented by the following syntax:

MIN HOUR DOM MON DOW Command

How to Set a Cron Job:

Basic Commands:

  • crontab -e: Opens the crontab file in the default text editor specified by your system.

  • crontab -l: Lists all the cronjobs of the current user, displaying the contents of the crontab file along with scheduled tasks and their associated commands.

Example:

crontab -l

# OUTPUT

# Example cron job
* * * * * /path/to/your/command.sh

Setting Up Your First Cron Job:

  1. Use crontab -e to open your default text editor.
crontab -e
  1. Write the cronjob in the specified syntax. Example:
30 14 * * * /path/to/your/script.sh

This cronjob runs the process at 2:30 pm every day of the month, every month, and every week.

  1. Save and exit the editor.

  2. The cron job is now set up and will run according to the defined schedule. To view the cronjob, use crontab -l.

crontab -l

User Management:

  • What is a user? A user in Linux is an entity with the authority to execute tasks on the Linux machine and perform various operations. Each user is assigned a unique ID in the operating system.

Important Information:

  • ID 0 is assigned to the root user.

  • IDs 1 to 999 (inclusive) are allocated to system users.

  • IDs for local users start from 1000 onwards.

Question 1: Directory Creation

#!/bin/bash

# Check if three arguments are provided
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <directory_name> <start_number> <end_number>"
    exit 1
fi

# Assigning arguments to variables
directory_name=$1
start_number=$2
end_number=$3

# Loop to create directories with dynamic names
for ((i = start_number; i <= end_number; i++)); do
    mkdir -p "${directory_name}_${i}"
done

echo "Directories created successfully."

To use this script, run the following command:

./createDirectories.sh mydir 1 5

This will create directories named mydir_1, mydir_2, mydir_3, mydir_4, and mydir_5.

Question 2: User Account Setup

#!/bin/bash

# Creating user accounts user1 and user2
sudo useradd user1
sudo useradd user2

# Displaying the usernames
echo "User accounts created: user1, user2"

To use this script, run the following command:

./createUsers.sh

This will create two user accounts (user1 and user2) and display their usernames.

Question 3: Automate with Cron Job

#!/bin/bash

# Check if two arguments are provided
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <directory_name> <frequency_in_minutes>"
    exit 1
fi

# Assigning arguments to variables
directory_name=$1
frequency_in_minutes=$2

# Creating a cron job to run the createDirectories.sh script
cronjob="@reboot /path/to/createDirectories.sh $directory_name 1 5"
cronjob+="\n*/$frequency_in_minutes * * * * /path/to/createDirectories.sh $directory_name 1 5"

# Writing the cron job to crontab
echo -e "$cronjob" | crontab -

echo "Cron job set up successfully."

To use this script, run the following command:

./scheduleDirectoryCreation.sh mydir 10

This will create a cron job to run the createDirectories.sh script every 10 minutes with the specified directory name and range of numbers. Adjust the paths accordingly.

Conclusion :

That's a wonderful conclusion! I'm glad we could cover various aspects of shell scripting, Cron, and user management in Linux. Automation and scripting indeed play a crucial role in streamlining processes and enhancing efficiency.

Your quote beautifully captures the essence of DevOps and the continuous pursuit of improvement in software delivery.

If you have any more questions or if there's anything else you'd like to explore, feel free to ask. Happy learning, and may your journey in mastering Linux and shell scripting be both enjoyable and rewarding!

ย