Home > Package & System Management > crontab

Complete Guide to the crontab Command: How to Schedule Repetitive Tasks

`crontab` is a command used in Linux/Unix systems that allows users to schedule specific commands or scripts to run periodically at specified times. It plays a crucial role in automating repetitive tasks such as system maintenance, backups, and data synchronization.

Overview of crontab

crontab is short for 'cron table', which is a file that contains the scheduled tasks of a user. Each line in this file defines a single job, and the cron daemon reads this file to execute the tasks at the specified times.

Structure of crontab Fields

Each line of the crontab file consists of 6 fields. The first 5 fields define the execution time, and the last field defines the command to be executed.

Details of Time Specification Fields
Field Description Range
1 Minute 0-59
2 Hour 0-23
3 Day of month 1-31
4 Month 1-12
5 Day of week 0-7 (0 and 7 are Sunday)
6 Command The command or script to execute

Special Characters

You can use special characters in the time fields to specify time more flexibly.

  • `*`: Every time (e.g., every minute, every hour)
  • `,`: Specify multiple values (e.g., `1,3,5` -> minutes 1, 3, 5)
  • `-`: Specify a range (e.g., `10-15` -> from 10 minutes to 15 minutes)
  • `/`: Specify an interval (e.g., `*/10` -> every 10 minutes)

Main Options

Take a look at the key features of the crontab command, including crontab file management options.

1. Managing crontab Files

2. User Management

Generated command:

Try combining the commands.

Description:

`crontab` Executes the command.

Combine the above options to virtually execute commands with AI.

Commonly Used Examples

Learn how to set up various repetitive tasks using crontab.

Run backup script at 2:30 AM every day

crontab -e
30 2 * * * /home/user/backup.sh

Sets up to run the `/home/user/backup.sh` script at 2:30 AM every day.

System update at 8 AM every Monday

crontab -e
0 8 * * 1 sudo apt update && sudo apt upgrade -y

Updates system packages at 8 AM every Monday. (For Ubuntu/Debian)

Clean log file every 10 minutes

crontab -e
*/10 * * * * echo '' > /var/log/app.log

Deletes the contents of `/var/log/app.log` every 10 minutes.

Restart web server at a specific time

crontab -e
0 4 1 * * sudo systemctl restart nginx

Restarts the web server (nginx) at 4 AM on the 1st of every month.

Run task every hour from 6 AM to 6 PM

crontab -e
0 6-18 * * * /path/to/myscript.sh

Runs `myscript.sh` at the start of every hour from 6 AM to 6 PM.

Install crontab (if necessary)

Most Linux distributions have it installed by default, but in some minimal installations, you may need to install the cron daemon package.

Debian/Ubuntu

sudo apt update
sudo apt install cron

Installs the cron daemon on Ubuntu or Debian-based systems.

CentOS/RHEL/Fedora

sudo yum install cronie
sudo systemctl enable crond
sudo systemctl start crond

Installs the cron daemon on CentOS, RHEL, or Fedora-based systems.


Related commands

These are commands that are functionally similar or are commonly used together.


Same category commands