Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

crontab in Linux With Examples

Last Updated on December 11, 2023 by Abhishek Sharma

Crontab, a time-based job scheduler in Linux, provides a robust mechanism for automating repetitive tasks. Whether you are a system administrator looking to schedule routine maintenance or a developer automating script execution, understanding crontab is a valuable skill. In this article, we will delve into the basics of crontab, and its syntax, and provide practical examples to demonstrate its versatility.

What is crontab in Linux?

Crontab, short for "cron table," is a scheduling daemon that executes tasks at specified intervals. Each user on a Linux system can have their crontab, allowing for individualized automation. System-wide tasks can be managed in the system crontab.

Basic Syntax of Crontab

The crontab command has a straightforward syntax:

crontab [options] filename
  • Options: Modify the behavior of the crontab command.
  • Filename: Specifies the file containing the cron jobs.

Crontab Time Specification

Crontab schedules tasks using a five-field time specification:

* * * * *
│ │ │ │ │
│ │ │ │ └── Day of the week (0 - 6, where Sunday is 0)
│ │ │ └───── Month (1 - 12)
│ │ └─────── Day of the month (1 - 31)
│ └───────── Hour (0 - 23)
└─────────── Minute (0 - 59)

Asterisks (*) represent wildcard characters, meaning "every."

Practical Examples of Crontab in Linux Usage

Here are practical examples of Crontabl in Linux:

1. Running a Script Every Day at Midnight
To execute a script named every day at midnight, add the following line to your crontab:

0 0 * * * /path/to/

2. Running a Command Every 15 Minutes
To run a command every 15 minutes, use the following crontab entry:

*/15 * * * * /path/to/command

3. Scheduling a Weekly Backup
Performing a weekly backup every Sunday at 3 AM can be achieved with the following crontab entry:

0 3 * * 0 /path/to/

4. Running a Task on Specific Months and Days
To run a task only in March, June, and September on the 1st and 15th of each month at noon, you can use:

0 12 1,15 3,6,9 * /path/to/task

5. Redirecting Output and Logging
When scheduling tasks, it’s often useful to capture output and errors. Redirecting output to a file and logging events can be achieved like this:

0 2 * * * /path/to/ > /path/to/output.log 2>&1

This example runs a script at 2 AM and redirects both standard output and errors to a log file.

Managing Crontab Entries

crontab -e: Edit the user's crontab.
crontab -l: List the user's crontab.
crontab -r: Remove the user's crontab.

Crontab is a powerful tool that empowers Linux users to automate tasks with precision and efficiency. By understanding its syntax and exploring various examples, you can schedule routine tasks, automate backups, and streamline your workflow. Mastering crontab is an essential skill for both system administrators and users seeking to enhance their efficiency and productivity in a Linux environment. Experiment with different time specifications and commands to tailor crontab to your specific needs.

FAQs related to crontab In Linux

Here are some of the frequently asked questions related to crontab in Linux:

1. What is crontab, and how does it differ from other scheduling tools?
Crontab is a time-based job scheduler in Linux. It allows users to schedule tasks at specified intervals. Unlike other tools, crontab is native to Unix-like systems and provides a simple and flexible way to automate tasks.

2. How do I view the existing crontab entries for my user?
You can view the existing crontab entries for your user by using the command crontab -l. This command lists the currently scheduled jobs.

3. How do I edit my crontab entries?
To edit your crontab entries, use the command crontab -e. This command opens the crontab file in the default text editor, allowing you to add, modify, or delete scheduled tasks.

4. Can I redirect the output of a cron job to a file?
Yes, you can redirect the output of a cron job to a file. Include the redirection in the crontab entry, like this: 0 2 * /path/to/ > /path/to/output.log 2>&1. This example redirects both standard output and errors to a log file.

5. How do I schedule a job to run every hour?
To schedule a job to run every hour, use the crontab entry 0 /path/to/job. This specifies that the job should run at the 0th minute of every hour.

6. Can I use crontab to schedule tasks for specific months or days of the week?
Yes, crontab allows you to schedule tasks for specific months and days of the week. For example, 0 12 1,15 3,6,9 * /path/to/task runs a task at noon on the 1st and 15th of March, June, and September.

Leave a Reply

Your email address will not be published. Required fields are marked *