Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User Management
- Create Directories Using Shell Script:
Write a bash script
createDirectories.sh
that, when executed with three arguments (directory name, start number of directories, and end number of directories), creates a specified number of directories with a dynamic directory name.Example 1: When executed as
./
createDirectories.sh
day 1 90
, it creates 90 directories asday1 day2 day3 ... day90
.Example 2: When executed as
./
createDirectories.sh
Movie 20 50
, it creates 31 directories asMovie20 Movie21 Movie22 ... Movie50
.-
2.Backups are an important part of a DevOps Engineer's day-to-day activities. The video in the references will help you understand how a DevOps Engineer takes backups
What is Cron?
Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule jobs (commands or scripts) to run automatically at specified intervals. This could be anything from running a backup script every night at 2 AM, to sending out reminder emails every Monday morning, or cleaning up log files every hour.
Cron is started when the system boots up and runs in the background, checking the Crontab files to see if there are any jobs to execute.
What is Crontab?
Crontab (Cron table) is a configuration file that specifies shell commands to run periodically on a given schedule. Each user on a system can have their own Crontab file, allowing for personalized automation.
The Crontab file consists of a series of lines, each line representing a job, with the time and date fields followed by the command to be executed.
Crontab Syntax
A typical Crontab entry follows this format:
30 2 * * * /path/to/backup.sh
Editing Crontab
To edit your Crontab file, you use the crontab -e
command. This opens your Crontab file in the default text editor, allowing you to add, modify, or remove jobs.
Copy codecrontab -e
To view your current Crontab entries, use:
Copy codecrontab -l
To remove all Crontab entries for the current user, use:
Copy codecrontab -r
3. Read About Cron and Crontab to Automate the Backup Script:
- Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit, or delete entries to cron. A crontab file is a user file that holds the scheduling information.
Read About User Management:
A user is an entity in a Linux operating system that can manipulate files and perform several other operations. Each user is assigned an ID that is unique within the system. IDs 0 to 999 are assigned to system users, and local user IDs start from 1000 onwards.
Create 2 users and display their usernames.
Managing Users and Groups
Adding a User
To add a new user, use the
useradd
command:sudo useradd -m username
- The
-m
option creates a home directory for the user.
- The
Removing a User
To remove a user, use the userdel
command:
sudo userdel username