
cron Command in Linux
cron (derived from the Greek word "chronos" meaning time) is a powerful utility in Linux that enables you to schedule tasks or commands to run automatically at predefined times, intervals, or based on specific events. This automation streamlines system administration, freeing you from manual intervention for repetitive tasks.
The cron command in Linux is a powerful utility used to schedule tasks to be executed automatically at a specified time and date. This command is essential for system administrators and users who want to automate repetitive tasks, such as backups, system updates, or custom scripts.
Table of Contents
Here is a comprehensive guide to the options available with the cron command −
- Understanding cron Command in Linux
- Install cron Command in Linux
- How to Use cron Command in Linux?
- Examples of cron Command in Linux
- Alternatives of cron Command in Linux
Understanding cron Command in Linux
The cron daemon is a long-running process that executes commands at specified intervals. Each user on a system can have their own crontab file, which contains instructions for the cron daemon. These instructions are known as cron jobs.
- cron daemon − This background service (usually named cron or crond) continuously runs on the system, periodically checking crontabs for scheduled tasks to execute.
- crontab − A special file (one per user) that stores cron expressions defining when and what commands to run.
Prerequisite: Install cron Command in Linux
In most Linux distributions, the cron command (or more precisely, the cron daemon) is pre-installed. There's typically no separate installation required.
Here's how to verify if cron is already installed −
crontab -h

This command checks for the presence of crontab and displays its information if found. If you see the output, cron is installed.
If cron is unavailable for some reason, 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 use crontab to manage your scheduled tasks as described in the previous response.
How to Use cron Command in Linux?
The cron command itself has limited options, as it primarily functions as a launcher for the crontab utility, which manages cron jobs.
Syntax of crontab Entries
A crontab file contains lines of cron jobs, and each job is represented by a single line. The syntax for a cron job is as follows −
* * * * * command_to_execute
This consists of six fields separated by spaces. The first five are time fields that specify when the command should run, and the last field is the command itself.
- Minute − Values range from 0 to 59.
- Hour − Values range from 0 to 23.
- Day of the Month − Values range from 1 to 31.
- Month − Values range from 1 to 12, or you can use the first three letters of the month's name.
- Day of the Week − Values range from 0 to 6 (Sunday to Saturday), or you can use the first three letters of the day's name.
Special Characters in Time Fields −
- Asterisk (*) − Represents all possible values for a field.
- Comma (,) − Specifies a list of values.
- Hyphen (-) − Defines a range of values.
- Slash (/) − Specifies a step value.
While not technically cron options, these commands are helpful for managing cron jobs. Here's a breakdown of the available cron command options −
Options | Description |
---|---|
crontab -l | Lists the cron jobs (entries) in your crontab file. |
crontab -e | Open the crontab file for editing in your default text editor. If you don't have a crontab file yet, it will be created for you. |
crontab -i | Starts an interactive shell where you can directly create or edit cron job entries. |
crontab -r | Removes all your cron job entries from the crontab file. |
crontab -f file | Specifies an alternative crontab file to edit or list entries from (usually not needed for most users). |
Additional Cron Commands | |
crontab -v | Check the installed crontab version. |
service cron status or systemctl status cron | Check the status of the cron daemon (running, stopped, etc.). |
service cron start or systemctl start cron | Starts the cron daemon if it's stopped. |
service cron stop (or systemctl stop cron) | Stop the cron daemon (use it with caution as it will prevent scheduled jobs from running). |
Examples of cron Command in Linux
Now, lets go through some examples to better understand the working of the cron command in Linux −
- 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
- Run a script at 10 AM on the second Sunday of April
- View your crontab file
- Edit crontab File
- Remove crontab file
Run a script every minute
The line * * * * * /path/to/script.sh 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 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

Note − Remember to replace command or 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 script.sh only once at 8:20 PM on February 28th of every year −
0 20 28 2 * script.sh

Note − This assumes February has 28 days.
Run a script at 10 AM on the second Sunday of April
This cron expression runs /path/to/script.sh only once a month, at 12:00 AM (midnight) on the first Friday of the month. It checks the current week number (using date +%U) and executes the script only if it's equal to 1 (first week) −
0 10 * 4 0 [ $(date +\%U) -eq 1 ] && script.sh

This uses a combination of cron and a shell test to determine the second Sunday.
By effectively utilizing cron, you can automate various tasks in Linux, enhancing system efficiency and reducing manual workload.
View your crontab file
Managing cron Jobs To view your crontab file, use −
crontab -l is a command in Linux that displays a list of all the automated tasks (cron jobs) you have currently scheduled in your crontab file, in just two lines. It essentially shows you what commands will run and when.
crontab -l

Edit crontab File
To edit your crontab file, use crontab -e opens the crontab file, which stores your scheduled tasks (cron jobs), for editing in your default text editor. This allows you to define when and what commands to run automatically on your Linux system.
crontab -e

Remove crontab file
To remove your crontab file, use the command crontab -r removes all your scheduled tasks (cron jobs) stored in the crontab file, essentially clearing your automated task list in two steps -r: Specifies the "remove" option. crontab: Launches the crontab utility for managing cron jobs.
crontab -r

Alternatives of cron Command in Linux
While cron is a powerful and widely used tool, there are several alternatives for scheduling tasks in Linux, depending on your specific needs −
at
The syntax is simpler for one-time tasks than for cron's expressions. Useful for scheduling tasks at specific times in the future.
systemd timers
Part of the systemd service manager commonly used in newer Linux distributions. Offers cron-like scheduling but with additional features like dependencies and unit files. More complex setup but provides greater control over service management.
Anacron
Ideal for systems that aren't always running (e.g., laptops). Runs missed cron jobs when the system boots up. Ensures tasks are executed even if the system is down during the scheduled time.
Conclusion
The cron command is an indispensable tool for automating tasks in Linux. With a proper understanding of its syntax and special characters, you can schedule tasks efficiently and ensure your system runs smoothly without manual intervention.
Remember, while cron is powerful, it's important to use it responsibly to avoid overloading your system with too many tasks. Running a script this frequently can consume system resources. If your script doesn't require such continuous execution, you might want to adjust the cron expression to a less frequent interval.