Day 5: Advanced Linux Shell Scripting for DevOps Engineers with User Management

Day 5: Advanced Linux Shell Scripting for DevOps Engineers with User Management

What is Cron?

Cron is a service used for scheduling tasks in the system that allows automation for a particular interval of time which is manually difficult to do regularly. It is used for the automation of repetitive tasks such as backups, and system maintenance.

What is a Cronjob?

A Cronjob is a task or command scheduled to run at a specific date and time. It automates repetitive tasks and manages the system more efficiently. It runs the scripts to backup data, and perform system updates and maintenance tasks.

What is Crontab?

Crontab is a file in which there is a set of commands that tells the system at what interval of time and when the system has to perform the task. It defines cronjob and their schedules. It is also known as the "cron table" . It is used for editing and scheduling tasks.

Here we can see how crontab looks.

By using cat /etc/crontab command we can see the crontab. It is explained in detail by the use of 5 asterisks (*****) In which the first asterisk position is for the minute, the second asterisk for the hour, the third asterisk is a day of the month, the fourth asterisk represents months and the fifth asterisk represents the day of the week.

If I have to see crontab then we can use the command crontab -l .Here we don't have any crontab so we can create a new one.

For editing a crontab you have to write crontab -e. If you are a new user it will first ask for an editor to select you can select it by putting the option number of your favorite editor you want to work on.

Here is the interface when you open the editor. After that, we are making on cronjob file named test_cron.txt. In this cronjob we want it to run only in 1st month which is why we write the 4th asterisk as 1 and the rest asterisks mean that we want our cronjob to run every minute every hour every day of the first month no matter what day it is.

Now we can print what is inside our cronjob file.

It will give a detailed explanation of the crontab task.

What is user management?

User management is used for handling user accounts, permissions, and access controls across various systems and services. Each user has a unique ID.

Q. Create 2 users and just display their Usernames.

-> By using useradd we can add a user. we need to take care of that when we create a user we must be in the root account or otherwise we have to use the sudo command. for ex: sudo useradd rohit

-> id rohan will tell the unique ID of user Rohan and group details.

-> Here users are created and we can view them through cat /etc/passwd command.

Q. You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments -

So Write a bash script create directories.sh that when the script is executed with three given arguments (one is the directory name second is the start number of directories and the third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.

Example 1: When the script is executed as

./createDirectories.sh day 1 90then it creates 90 directories as day1 day2 day3 .... day90. It will create directories from 1 to 90.

we will first start from creating a script file using nano editor by writing the command nano directories.sh

-> This is how we write the script for creating the directory as much as we want. let's understand how I wrote this script in detail.

-> $1, $2, and $3 are the arguments assigned to the variables of the script.

-> Using a for loop we give a range of numbers to our script.

-> "${dir_name}${i}": It will concatenate the directory name with the current value of i. For example, if there dir_name is day and i value is 2 then our directory name will be day2. Their value is assigned to the variable name directory.

-> mkdir -p "$directory" It will create the directory specified by the variable directory and -p ensures that the parent directories are created if they don't exist.

-> echo "$directory" It will print the directory names.

-> We have to give permission first before executing the shell so that the file will run smoothly.

Note: We have to give the values of arguments after the name of the file after that It will create directories from 1 to 90.

Here we can see the list of all 90 directories.

Summary:

These are some of the concepts regarding cron, cronjob, and crontab in which we learn how to create crontabs. Next, we see the user management concept which is used for handling user accounts. In the last, we saw one question related to creating directories in detail.