crontab Command in Linux: Practical Examples & Cheat Sheet

crontab Command in Linux: Practical Examples & Cheat Sheet

crontab is a Linux command used to schedule commands or scripts to run automatically at fixed times or intervals. These scheduled tasks are called cron jobs and are executed by the cron daemon running in the background.

crontab is widely used for system maintenance, backups, log rotation, and automation tasks that must run without manual intervention.


How cron and crontab Work

Think of cron as the background scheduler and crontab as its schedule file.

time rules  →  cron service  →  command or script
  • cron runs continuously in the background
  • crontab defines what task to run and when to run it
  • Each line in crontab represents one scheduled job

This model helps understand why incorrect timing or paths cause cron jobs to fail silently.


Crontab File Locations and Access Control

Linux allows administrators to control who can use cron.

/etc/cron.allow
/etc/cron.deny
  • If /etc/cron.allow exists, only listed users can use cron
  • If it does not exist, users not listed in /etc/cron.deny can use cron
  • If neither file exists, only root is allowed to schedule cron jobs

This prevents unauthorized users from running background jobs.


Crontab Syntax and Time Format

Each cron job consists of five time fields followed by the command to execute.

MIN  HOUR  DOM  MON  DOW  COMMAND
Field Meaning Allowed Values
MIN Minute 0–59
HOUR Hour 0–23
DOM Day of Month 1–31
MON Month 1–12 or jan–dec
DOW Day of Week 0–6 or sun–sat

Common patterns used in cron schedules:

  • * → every value (every minute, hour, day)
  • , → list (1,5,10)
  • - → range (1–5)

Understanding this format makes it easier to read and debug cron jobs.


Managing User Crontabs

Use the crontab command to view, edit, or manage scheduled jobs for a user.

View current user’s cron jobs

crontab -l

If no cron jobs exist, cron explicitly reports that no crontab is configured for the user.

Edit current user’s cron jobs

crontab -e

This opens the user’s crontab file in the default editor, where jobs can be added, updated, or removed.

Each line represents one scheduled task.


Common Cron Scheduling Examples

These are the most frequently used cron schedules in real systems.

Run a job at a specific date and time

45 07 14 01 * cp -R /home/user/data /home/user/data_backup

Runs once at 7:45 AM on January 14.

Run a job every day at a fixed time

0 20 * * * myscript.sh

Runs daily at 8:00 PM.

Run a job multiple times per day

0 7,21 * * * myscript.sh

Runs every day at 7 AM and 9 PM.

Run multiple commands in a single cron job

0 6 * * * script1.sh; script2.sh

Commands are executed sequentially in the same schedule.


Special Cron Time Strings (Shortcuts)

Cron provides predefined keywords for common schedules.

@yearly    @monthly    @weekly    @daily    @hourly

Example:

@daily myscript.sh

These are easier to read and reduce syntax mistakes.

Run a job once per day

@daily backup.sh

Ideal for:

  • Daily backups
  • Log cleanup
  • Health checks

Equivalent to:

0 0 * * *

Run a job once per week

@weekly cleanup.sh

Commonly used for:

  • Disk cleanup
  • Weekly reports
  • Maintenance tasks

Equivalent to:

0 0 * * 0

Run a job once per month

@monthly billing.sh

Useful for:

  • Monthly reports
  • Billing or accounting scripts
  • Data archival

Equivalent to:

0 0 1 * *

Run a job once per year

@yearly archive.sh

Suitable for:

  • Annual archiving
  • License renewal checks
  • Yearly audits

Equivalent to:

0 0 1 1 *

Run a job every hour

@hourly sync.sh

Good for:

  • Data synchronization
  • Cache refresh jobs
  • Monitoring scripts

Equivalent to:

0 * * * *

Run a job at system startup

@reboot init_checks.sh

Useful for:

  • Service validation after reboot
  • Re-mounting network storage
  • Recovery or initialization tasks

Note: @reboot jobs run only when the cron service starts, not on every login.


Managing Other Users’ Crontabs (Root Only)

System administrators can manage cron jobs for other users using the -u option. These commands require root privileges.

View another user’s crontab

sudo crontab -u username -l

Lists all cron jobs configured for the specified user.

Edit another user’s crontab

sudo crontab -u username -e

Opens the user’s crontab in the editor for modification.

Remove another user’s crontab

sudo crontab -u username -r

Prompt before removal:

sudo crontab -u username -i -r

Use caution when removing crontabs, as all scheduled jobs for the user will be deleted.

* * * * * /path/script.sh >> /tmp/cron.log 2>&1

This captures both standard output and errors, making troubleshooting much easier.


Frequently Asked Questions

1. What is crontab in Linux?

crontab is a Linux command used to create, edit, list, and remove cron jobs, which are scheduled tasks executed automatically at specified times.

2. Where are crontab files stored?

User crontab files are stored under /var/spool/cron/crontabs and should only be edited using the crontab command.

3. How do I check if a cron job is running?

You can verify cron execution by checking system logs such as /var/log/syslog or /var/log/cron depending on the distribution.

4. Why is my cron job not running?

Common reasons include missing environment variables, incorrect paths, permission issues, or syntax errors in the crontab entry.

Conclusion

The crontab command is a powerful and reliable tool for automating tasks in Linux. By understanding cron syntax, scheduling patterns, and common pitfalls, you can confidently schedule jobs that run in the background without manual intervention.

From simple daily scripts to system maintenance and monitoring tasks, crontab remains an essential skill for Linux users and system administrators.


Also Read


Further Reading

Rohan Timalsina

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.