crontab

system schedulingLinux/Unix
The crontab command is one of the most frequently used commands in Linux/Unix-like operating systems. crontab Schedule and manage cron jobs

Quick Reference

Command Name:

crontab

Category:

system scheduling

Platform:

Linux/Unix

Basic Usage:

crontab [options] [arguments]

Common Use Cases

  • 1

    Scheduled backups

    Automate regular backup tasks at specific times

  • 2

    System maintenance

    Schedule routine maintenance tasks like log rotation or cleanup

  • 3

    Periodic reporting

    Generate and send reports on a regular schedule

  • 4

    Automated monitoring

    Run system checks and monitoring scripts at defined intervals

Syntax

crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i]

Options

Option Description
-l List the current crontab
-r Remove the current crontab
-e Edit the current crontab
-i Prompt for confirmation before removing crontab
-u user Specify the user whose crontab to manage (requires appropriate privileges)

Examples

How to Use These Examples

The examples below show common ways to use the crontab command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

# List your cron jobs
crontab -l
# Edit your crontab file crontab -e
# Remove all your cron jobs crontab -r
# Install a new crontab from a file crontab mycron.txt

Advanced Examples:

# List another user's crontab (requires root privileges)
sudo crontab -u username -l
# Edit another user's crontab (requires root privileges) sudo crontab -u username -e
# Remove crontab with confirmation crontab -i -r # Backup your crontab to a file crontab -l > crontab_backup.txt # Restore crontab from a backup file crontab crontab_backup.txt # Create a crontab with common schedule examples
cat > mycron.txt << EOF
# Run at 5:30am every day 30 5 * * * /path/to/script.sh # Run every hour 0 * * * * /path/to/hourly_script.sh # Run every 15 minutes */15 * * * * /path/to/frequent_script.sh # Run at midnight on the first day of each month 0 0 1 * * /path/to/monthly_script.sh # Run every weekday at 6pm 0 18 * * 1-5 /path/to/weekday_script.sh EOF crontab mycron.txt

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

The crontab command is used to view or edit the cron table, which contains scheduled tasks (cron jobs) that will be executed at specified times. Cron jobs are used for system maintenance or administration tasks, such as backups, system updates, or cleaning temporary files.

Key points about crontab:

  • Each user on a system can have their own crontab
  • The system administrator can access or modify any user's crontab using the -u option
  • Changes to crontab take effect immediately; no need to restart any service
  • The crontab file is stored in a system location (usually /var/spool/cron/crontabs/)
  • Most systems provide access control through /etc/cron.allow and /etc/cron.deny files

Crontab Format:

Each line in a crontab file follows this format:

# Minute Hour Day-of-Month Month Day-of-Week Command
#  (0-59) (0-23)   (1-31)   (1-12)   (0-7)
    30     2        *        *        1      /path/to/script.sh

Special time strings:

  • @reboot: Run once at startup
  • @yearly or @annually: Run once a year (0 0 1 1 *)
  • @monthly: Run once a month (0 0 1 * *)
  • @weekly: Run once a week (0 0 * * 0)
  • @daily or @midnight: Run once a day (0 0 * * *)
  • @hourly: Run once an hour (0 * * * *)

Common crontab patterns:

  • * * * * *: Run every minute
  • */5 * * * *: Run every 5 minutes
  • 0 * * * *: Run at the beginning of every hour
  • 0 0 * * *: Run at midnight every day
  • 0 0 * * 0: Run at midnight every Sunday
  • 0 0 1 * *: Run at midnight on the first day of each month

Important tips when using crontab:

  • Always include the full path to commands and files in your cron jobs
  • Redirect output to a file or to /dev/null to prevent email notifications
  • Set appropriate environment variables as they may differ from your login environment
  • Test your commands manually before adding them to crontab
  • Use absolute paths in your cron jobs
  • Ensure scripts have appropriate permissions (chmod +x)
  • For debugging, redirect both stdout and stderr to a log file: command > /path/to/log 2>&1

Tips & Tricks

1

Use the -e option to edit the crontab file

2

Use the -l option to list the crontab file

3

Use the -r option to remove the crontab file

4

Use the -u user option to specify the user whose crontab file to edit

5

Use the -i option to prompt before removing the crontab file

Common Use Cases

Scheduled backups

Automate regular backup tasks at specific times

System maintenance

Schedule routine maintenance tasks like log rotation or cleanup

Periodic reporting

Generate and send reports on a regular schedule

Automated monitoring

Run system checks and monitoring scripts at defined intervals

Task automation

Schedule any repetitive task to run without manual intervention

Related Commands

These commands are frequently used alongside crontab or serve similar purposes:

Use Cases

1

Scheduled backups

Automate regular backup tasks at specific times

2

System maintenance

Schedule routine maintenance tasks like log rotation or cleanup

3

Periodic reporting

Generate and send reports on a regular schedule

4

Automated monitoring

Run system checks and monitoring scripts at defined intervals

5

Task automation

Schedule any repetitive task to run without manual intervention

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the crontab command works in different scenarios.

$ crontab
View All Commands