Introduction
In this blog we'll be diving deep into the concepts of shell scripting along with learning new ways to use it for automating tasks. We'll also learn about Cron and Crontab and User management in Linux. Also we'll recall the concepts learned in the past few blogs and have some hands on practice by solving some interactive scenario based questions. So let's begin learning.
What is Cron?
Cron is a service provided by Linux which helps us in scheduling tasks and automating them. It is a Unix based job-scheduler that schedules and performs tasks according to the instruction provided to it.
Suppose we need to take keep a track and backup the logs exactly at 3 am. Instead of doing it manually, we can set a "Cron Job" that does it for us.
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.
Mostly it is used for keeping a track of logs, system maintenance and other routine operations, enabling users to automate processes without manual intervention.
Important: A Cron Job is managed by the cron daemon.
What is a crontab?
A crontab is a configuration file that holds the set of instruction for a cron job. Crontab stands for βcron table" . The crontab file contains lines with scheduling information and the corresponding commands to be executed.
Important: Crontabs are stored in /etc/crontab folder.
Understanding the Crontab Syntax
The Linux Crontab Format is represented by the following syntax:
MIN HOUR DOM MON DOW Command
Field | Description | Range |
MIN | Minute. Specifies the minute for the command to run | 0-59 min |
HOUR | Specifies the hour of the day when the command will run | 0-23 hr |
DOM | Day of Month. Specifies the day of the month for the task. | 1-31 |
MON | Month. For the month in which the command will run | 1-12 |
DOW | Day of Week. Specifies the day of the week for the tasks to run. | 0 - 6 |
Command | 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.
crontab -l #OUTPUT # Example cron job * * * * * /path/to/your/command.sh
Setting up your first cronjob:
Step - 1
use crontab -e to open your default text editor.
crontab -e
Step -2
Write the cronjob in the syntax mentioned above. Example:
30 14 * * * /path/to/your/script.sh
This Cronjob means that the process will run at 2:30 pm on every day of the month, every month and every week.
Step - 3
Save and exit the editor.
The cron job is now set up, and it will run according to the schedule you defined. To View the cronjob, use crontab -l.
crontab -l
User Management
What is a user?
A user is an entity in Linux that has the power to execute and perform tasks on the linux machine and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system.
IMPORTAN: ID 0 is assigned to the root user and the IDs 1 to 999 both included are assigned to the system users and hence the ids for local user begins from 1000 onwards.
System User Vs Local User:
System User | Local User |
System users are created to run specific system processes or services. They are not meant for interactive logins. | Local users are created for human users who interact with the system. They may have home directories and can log in to the system. |
No home directory | Local users usually have a home directory where they can store personal files and configurations. |
No Interactive Login | Local users can log in and interact with the system directly |
services like Apache, Nginx, or databases are examples of System users. | Humans who needs access to the system are example of Local Users. |
User Management Commands:
Creating user:
To create a new User, use the "useradd" command.
sudo useradd <username>
Setting Password for the user:
To set password for the user, use "passwd" command.
sudo passwd <username>
This will allow you to set the password for the given user.
IMPORTANT: Remember to use sudo
to execute commands with administrative privileges.
Scenario: Bash Script Challenge
You are tasked with creating a bash script that automates the process of creating directories with dynamic names. Your script should take three arguments: the base name of the directories, the starting number, and the ending number. Additionally, you need to create a script to set up user accounts and display their usernames.
Question 1: Directory Creation
Write a bash script named createDirectories.sh
. When executed with three arguments (directory name, start number, and end number), the script should create a specified number of directories with dynamic names.
Question 2: User Account Setup
Write a separate bash script named createUsers.sh
. This script should create two user accounts (user1
and user2
) and display their usernames.
Question 3: Automate with Cron Job
Extend the automation by adding a cronjob to regularly execute the createDirectories.sh
script at a specific time.
Write a bash script named scheduleDirectoryCreation.sh
. This script should:
Accept two arguments - the directory name and the frequency in minutes.
Create a cronjob that runs the
createDirectories.sh
script with the specified directory name and a range of numbers.
Question 1:
Solution
Output:
Question 2:
Output:
To check a user we need to see it in the /etc/passwd directory.
Question 3 :
Output:
Conclusion:
In summary, we've explored shell scripting, Cron, and user management in Linux. We've learned to schedule tasks, automate processes, and tackled a Bash Script Challenge. By mastering these skills, we're equipped to enhance efficiency in system administration through automation. Let's continue our journey of mastering the power of shell scripting in Linux.
Ending this with a quote,
"In the world of DevOps, collaboration is the code, automation is the rhythm, and continuous improvement is the melody, orchestrating a symphony of efficiency in software delivery."
Happy Learning.