
crontab Command in Linux
Crontab stands for 'cron table', and it allows users to schedule commands or scripts to run at specific times and intervals. It's an essential utility for system administration, automating tasks such as backups, system maintenance, and other repetitive tasks that need to run on a schedule.
The crontab command in Linux is a powerful tool used for scheduling tasks to be executed automatically at predetermined times. It is an essential utility for system administration, allowing for the automation of repetitive tasks such as backups, system updates, and custom scripts.
Here, we delve into the various options of the crontab command, providing a comprehensive guide for both beginners and experienced users.
Table of Contents
Here is a comprehensive guide to the options available with the crontab command −
- Understanding crontab Command in Linux
- Install crontab Command on Linux
- How to Use crontab Command in Linux
- Examples of crontab Command in Linux
- Alternatives of crontab Command in Linux
Understanding crontab Command in Linux
The crontab command in Linux is a powerful tool that allows users to schedule tasks to be executed automatically at specified times. The name crontab is derived from the word 'cron', which itself is named after 'chronos', the Greek word for time. The tab part of the word stands for table, which is quite fitting as the crontab command deals with a table where each line represents a scheduled job.
The crontab file consists of lines of text, each representing a scheduled task. Each line has five time-and-date fields, followed by a command or script to be executed. The fields are as follows −
- Minute (0 - 59)
- Hour (0 - 23)
- Day of the Month (1 - 31)
- Month (1 - 12)
- Day of the Week (0 - 6, Sunday = 0)
Prerequisite: Install crontab Command in Linux
If for some reason cron is not available, you can install it using the package manager for your specific Linux distribution. Here are some general examples −
For Debian/Ubuntu
sudo apt-get update sudo apt-get install cron
For Red Hat/CentOS/Fedora
sudo yum update sudo yum install cronie
For OpenSUSE
sudo zypper update sudo zypper install cron
Once you've verified or installed cron, you can start using crontab to manage your scheduled tasks as described in the previous response.
How to Use crontab Command in Linux?
The syntax for crontab is a series of fields separated by spaces, where each field represents a different unit of time −
MIN HOUR DOM MON DOW CMD
- MIN − Minute field, ranges from 0 to 59.
- HOUR − Hour field, ranges from 0 to 23.
- DOM − Day of Month, ranges from 1 to 31.
- MON − Month field, ranges from 1 to 12.
- DOW − Day Of Week, ranges from 0 to 6 (Sunday can be represented by 0 or 7).
- CMD − The command or script that you want to run.
Here's a breakdown of the command and its various options −
Options | Descriptions |
---|---|
crontab -e | This option opens your crontab file in a text editor for editing. If the crontab file doesn't exist, it will create one. |
crontab -l | This option lists the contents of your crontab file, displaying the scheduled tasks. |
crontab -r | This option removes your crontab file, effectively disabling all scheduled tasks. |
crontab -i | This option prompts for confirmation before removing your crontab file. It's recommended to use this flag with -r to prevent accidental deletion. |
crontab -f file | Specifies an alternative crontab file to edit or list entries from (usually not needed for most users). |
Examples of crontab Command in Linux
Now, lets go through some examples to better understand the working of the crontab command in Linux −
- Edit the Crontab File
- Displays the Current Crontab Entries
- Removes the current user's crontab
- Cancel the Operation
- Displays the Help Page
- Run a Script Every Minute
- Run a script hourly
- Run a Script at 3 AM Every Day
- Backup Files Daily at Midnight
- Update Software Packages Weekly on Sundays at 2 AM
- Run a script at 4:30 PM on the first day of each month
- Run a script every 15 minutes
- Run a script at 2 AM on weekdays (Monday to Friday)
- Run a script at 8 PM on the last day of February
Edit the Crontab File
This crontab -e option is used to edit the crontab file for the current user. If the file does not exist, it creates a new one. This is the most commonly used option as it allows users to define and modify their scheduled tasks −
crontab -e

Displays the Current Crontab Entries
The crontab -l list option displays the current crontab entries for the user. It's a quick way to review what tasks are scheduled without making any changes.
crontab -l

Removes the current user's crontab
This crontab -r removes the current user's crontab file. It is a critical command and should be used with caution, as it will delete all scheduled tasks without any confirmation.
crontab -r

Cancel the Operation
Similar to the remove option, but with a safety net. It prompts the user before the actual removal of the crontab file, providing a chance to cancel the operation if it was initiated by mistake.
crontab -i

Displays the Help Page
Although not available on all systems, this crontab -h option displays the help page −
crontab -h

Run a Script Every Minute
The line * * * * * represents a cron expression that defines when and what command to run using cron. Lets run a command every minute −
* * * * * script.sh

This is the actual command or script that will be executed according to the cron expression. Replace this with the actual path to your script on the system.
Run a script hourly
This cron expression runs the script /path/to/your/script.sh every minute, because * in each field represents all possible values. In other words, the script runs continuously.
0 * * * * script.sh

Run a Script at 3 AM Every Day
This cron expression runs the script at /path/to/script.sh at 3:00 AM every day. Here, 0: Minute (0 for the 0th minute, meaning on the hour). 3 * * * *: Runs the script at 3 AM (* for every hour, day of month, month, and weekday).
0 3 * * * script.sh

Backup Files Daily at Midnight
This cron expression runs a script at midnight (0 0) every day (* * *) to create a compressed backup archive (.tar.gz) named with the current date using tar.
0 0 * * * /bin/tar -zcvf /home/user/backup_$(date +%Y-%m-%d).tar.gz /home/user/important_files

This example uses tar to create a compressed backup archive named with the current date and stores it in /home/user/backup.
Update Software Packages Weekly on Sundays at 2 AM
This cron expression instructs your system to run the command apt update && apt upgrade (update package lists and then upgrade packages) at exactly 2:00 AM every Sunday.
0 2 * * 0 apt update && apt upgrade

This assumes you use apt for package management.
Run a script at 4:30 PM on the first day of each month
This cron expression runs /path/to/script.sh at 4:30 PM (16:30) on the 1st day of every month.
30 16 1 * * script.sh

Run a script every 15 minutes
This cron expression (*/15 * * * * /path/to/script.sh) runs the script at /path/to/script.sh every 15 minutes because */15 in the minute field specifies running every 15th minute (0, 15, 30, 45). The remaining *s indicate all other time units (every hour, day, month, weekday).
*/15 * * * * script.sh

Run a script at 2 AM on weekdays (Monday to Friday)
This cron expression runs /path/to/script.sh at exactly 2:00 AM every weekday (Monday-Friday) −
0 2 * * 1-5 script.sh

Remember to replace /path/to/your/command or /path/to/your/script.sh with the actual paths to your commands or scripts.
Run a script at 8 PM on the last day of February
This cron expression runs the script /path/to/script.sh only once at 8:20 PM on February 28th of every year.
0 20 28 2 * script.sh

When working with crontab, it's important to follow best practices to ensure reliable task scheduling −
- Always backup your crontab file before making changes.
- Use absolute paths for commands and scripts to avoid any path-related issues.
- Test your crontab entries to ensure they work as expected.
- Consider redirecting output to a file or mailing it to yourself for record-keeping and debugging.
Alternatives of crontab Command in Linux
Crontab is a widely used tool for scheduling tasks in Linux, but there are several alternatives that might better suit your needs depending on the situation.
systemd timers
systemd is a widely adopted init system (service manager) on many Linux distributions. It provides a built-in mechanism for scheduling tasks using timers. systemd timers offer features like dependency management, precise execution times, and integration with systemd services.
Anacron
Anacron is specifically designed for scheduling tasks on systems that aren't running 24/7. It checks for missed scheduled tasks upon system boot and runs them accordingly. This is useful for laptops or servers that might be turned off periodically.
GUI-based Schedulers
Many desktop environments offer graphical user interface (GUI) based schedulers. These tools allow you to configure tasks visually, often without needing to write complex cron expressions.
Conclusion
crontab is powerful, it requires a responsible approach to avoid accidental deletions or misconfigurations.
Crontab is an indispensable tool for Linux users, especially for those who manage servers or perform system administration tasks. It provides a simple yet robust way to automate tasks and ensure that important jobs are run at regular intervals without manual intervention.
By mastering the crontab command, you can automate your workflows, save time, and reduce the risk of human error in your daily operations. Whether you're a seasoned system administrator or a new Linux user, the crontab command is a valuable skill to learn and integrate into your toolkit.