Day5- Devops Journey

Advanced Linux Shell Scripting for DevOps Engineers with User management.

What is Cron?

  • It is a service provided by Linux which helps to scheduling tasks and automating them. It is a Unix based job-scheduler that schedules and performs tasks according to the instruction provided to it.

What is a Cron Job?

  • A Cron Job is a set of instructions or Linux Commands given to the Cron in order to schedule tasks. It allows users to automate and perform repetitive tasks at a given time, date and frequency at which a particular command, script, or program should be executed.

  • A Cron Job is managed by the cron daemon.

What is Cron tab?

  • The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list.

  • Crontab stands for “cron table, ” because it uses the job scheduler cron to execute tasks; cron itself is named after “chronos".

  • Cron tabs are stored in /etc/crontab folder.

Cron tab Syntax:

Schedule Cron Job Every 1 hour in Linux - Fedingo

The Linux Crontab Format is represented by the following syntax:

  • MIN HOUR DOM MON DOW CMD

  • Field

    Description

    Allowed Value

    MIN (Minute)

    Specifies the minute when the command will run

    It ranges from 0 to 59.

    HOUR

    Denotes the hour of the day when the command is scheduled to execute.

    It spans from 0 to 23.

    DOM (Day of Month)

    Specifies the day of the month for the task.

    It ranges from 1 to 31.

    MON (Month)

    Indicates the month during which the command will be executed.

    It varies from 1 to 12.

    DOW (Day of Week)

    Specifies the day of the week for the task.

    It is represented by numbers from 0 to 6, where both 0 and 6 correspond to Sunday.

    CMD (Command)

    Represents the actual command or script that will run at the scheduled time.

    --

    How to set a Cron Job?

    Some basic commands:

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

    • crontab -l : This command lists all the cronjobs of the current user. It displays the contents of your crontab file, showing the scheduled tasks and their associated commands.

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 2nd month which is why we write the 4th asterisk as 2 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.

After every min., it will create file test_cron.txt and print his is my first cron job.

Here is detailed explanation of the schedule crontab task.

Task: What is 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 for each user in the operating system.

  • After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users and hence the ids for local user begins from 1000 onwards.

Task: Create 2 users and just display their Usernames.

  • Command to create user

    #sudo useradd vivek

    #sudo useradd linux

    #id vivek

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

    Last 2 line, shows user that i have created.

Task:

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

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

#vim createDirectory.sh

#chmod 777/755 createDirectory.sh

#./createDirectory day 1 90

Explanation:

  • This is how we can write the script for creating the directory as much as we want. let's understand 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.

  • Here is list of all 90 direcotries.

Thank you for reading my blog:)