Advanced Linux Shell Scripting for DevOps Engineers with User management

Advanced Linux Shell Scripting for DevOps Engineers with User management

#90 Days of DevOps Challenge - Day 5

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

#!/bin/bash

for ((i=$2;i<=$3;i++))
do
   mkdir $1$i
done

OutPut 1:-

OutPut 2:-

  • Create a Script to backup all your work done till now.

#!/bin/bash

src_dir=/home/ubuntu/scripts
tgt_dir=/home/ubuntu/backups

curr_time=$(date "+%d-%m-%H-%Y")
echo "Backup initated for $curr_time"
backup_file=$tgt_dir/$curr_time
tar -vczf $backup_file.tar.gz $src_dir
echo "Backup Completed"

OutPut:-

  • About Cron and Crontab, to automate the backup Script

Cron:-Cron is a system that helps Linux users to schedule any task. However, a cron job is any defined task to run in a given time period. It can be a shell script or a simple bash command. Cron job helps us automate our routine tasks, it can be hourly, daily, monthly, etc.

Crontab:- Crontab, which is short for cron table, is a file containing the schedule of various cron entries that should be run at specified times. Another way of describing crontab is as a utility that enables tasks to run automatically at regular intervals in the background by the cron daemon

Some of the most common crontab commands are the following:

  • crontab -e <UserName>. It enables the user to edit the crontab file or create a new file. When editing is complete, the file gets copied into the crontab directory as that particular user's crontab file (see below).

  • crontab -l <UserName>. It lists the cron jobs and displays the contents of the user's crontab file.

  • crontab -r <UserName>. It removes the crontab file from the crontab directory.

  • crontab -v <UserName>. This command is not available on all systems. It displays the last time a user-edited their crontab file and lists the status of the user's cron jobs.

Crontab file syntax:-

A crontab file contains entries for each cron job, with each entry separated from the next by newline characters. Also, each entry contains six fields separated by spaces:

  1. Minute: Range from 0 to 59

  2. Hour: Range from 0 to 23

  3. Day of the month: Range from 1 to 31

  4. Month: Range from 1 to 12

  5. Day of the week: Range from 0 to 6, where 0 = Sunday

  6. Command (shell command)

Crontab in Linux: Job Scheduling EXAMPLES

  • 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.

  • Create 2 users and just display their Usernames

For creating/add user :-

Command:- useradd <username>

Ex:- useradd saikat

display Usernames:-

Command :-

cat /etc/passwd

or

cat /etc/passwd |egrep -i '<username>'

Ex:- cat /etc/passwd |egrep -i 'saikat|riju'

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any query or if any correction to be done in this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Did you find this article valuable?

Support Saikat Mukherjee's blog by becoming a sponsor. Any amount is appreciated!