Skip to main content

Command Palette

Search for a command to run...

#90DaysOfDevOps Challenge - Day 5 Cron and Crontab ,User Management

Updated
5 min read
#90DaysOfDevOps Challenge - Day 5 Cron and Crontab ,User Management

What is cron and crontab?

Cron and Cron Tab are essential components in Unix-like operating systems that enable users to schedule recurring tasks or jobs at specified intervals. They are commonly used for automating routine tasks, running scripts, and executing commands at specific times, days, weeks, or months.

1. Cron:

Cron is a time-based job scheduling daemon that runs in the background on Unix-like systems. It continually checks the system's crontab files to see if any scheduled tasks need to be executed. The cron daemon, commonly known as "cron," reads the crontab files and executes the commands or scripts as per the defined schedule.

Let's suppose that, as a DevOps engineer, you want to automate a particular task that has been executed repeatedly. But as a human, you cannot do a task again and again manually when there are hundreds of tasks, so here you want to schedule the task in a way that it will execute on a particular day and at a particular time,or you want that task to run automatically in the background.

So this is called automation. So cron is a service or utility that allows you to schedule tasks so that you can automate them.

2. Crontab:

Crontab (short for "cron table") is a text file that contains a list of scheduled commands or scripts. Each line in the crontab represents a separate job. Users can edit their crontab to schedule tasks, and each user on the system can have their own crontab. The crontab files are usually stored in the /var/spool/cron directory.

There are crontab files and these are the files that you want to schedule ona particular day or time of the year. If you want to run these tasks in the background so that they keep running until you stop them, then you have to create a file in which there will be a set of commands and tell when this task is executed, at what time it will be executed, how many times it will be executed. So all these things are kept in your crontab.

The syntax for crontab entries is as follows:

* command_to_be_executed

- - - - -

| | | | |

| | | | ----- Day of the Week (0 - 7) (Sunday is 0 or 7)

| | | ------- Month (1 - 12)

| | --------- Day of the Month (1 - 31)

| ----------- Hour (0 - 23)

------------- Minute (0 - 59)

Here's a quick explanation of the fields:

- Minute: The minute when the job will be executed (0 to 59).

- Hour: The hour when the job will be executed (0 to 23).

- Day of the Month: The day of the month when the job will be executed (1 to 31).

- Month: The month when the job will be executed (1 to 12).

- Day of the Week: The day of the week when the job will be executed (0 or 7 for Sunday, 1 for Monday, and so on).

Asterisks (*) are used as wildcards to match any value. For instance, using * in the "Month" field would mean the job should run in every month.

Here's an example of a simple crontab entry:

'''

30 2 * /path/to/my_script.sh

'''

This entry means that the script /path/to/my_script.sh will be executed at 2:30 AM every day.

To edit the crontab for the current user, you can use the command crontab -e. This will open the crontab file in the default text editor, allowing you to make changes and save them. To list the existing crontab entries, you can use crontab -l. Administrators can also edit the system-wide crontab file located at /etc/crontab to schedule tasks that apply to all users on the system.

What is user management in Linux?

User management in Linux refers to the process of creating, modifying, and controlling user accounts on a Linux system. It is an essential aspect of system administration, as it allows administrators to control access and privileges for different users.

The main components of user management in Linux include:

  1. User Accounts: A user account represents an individual user on the system. Each user account has a unique username and a numeric User ID (UID). User accounts are stored in the system's user database, usually located in the '/etc/passwd' file.

  2. Groups: Users can be organized into groups, which are used to simplify the process of granting permissions and managing access to files, directories, and other resources. Group information is typically stored in the '/etc/group' file.

  3. Passwords: Every user account has a password associated with it to protect the user's data and resources. The password hashes are stored in the '/etc/shadow' file, which is only accessible by the root user.

  4. Privileges: Linux uses a permissions system that allows administrators to define what actions users can perform on files and directories. The root user (superuser) has full administrative privileges, while regular users have limited permissions based on their group memberships and individual settings.

Common user management commands in Linux:

  • adduser or useradd: These commands are used to create new user accounts.

  • passwd: Allows users to change their passwords or administrators to set initial passwords.

  • usermod: Used to modify existing user accounts, such as changing their home directory or group membership.

  • userdel: Deletes a user account from the system.

  • groupadd: Creates a new group on the system.

  • groupmod: Used to modify existing groups.

  • groupdel: Deletes a group from the system.

For more advanced user management tasks, Linux administrators can also use graphical tools or user management software that provides a more user-friendly interface for managing users and groups.

Adding 2 users and displaying them in the terminal :

-m is used to manage the password

To see whether the user created or not we use the following command.

o/p of the above command. you can see in the last lines of the terminal the newly created users.

The article is a follow-up to earlier tasks Day 5 of the #90daysofdevopschallenge.

Cron and Crontab are tools used in Unix-like systems for scheduling and automating recurring tasks. Cron is a background process that checks Crontab files to execute scheduled commands or scripts at specific intervals defined in the Crontab.

User management in Linux involves creating, modifying, and controlling user accounts and groups on the system. Each user has a unique username, a User ID (UID), and a password stored as a hash. User accounts are organized into groups to manage permissions efficiently. The root user has full administrative privileges. Common commands like adduser, passwd, and usermod are used for user management tasks.