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.


crontab Command - Quick Cheat Sheet

Taskcrontab Command
Edit current user's cron jobscrontab -e
List current user's cron jobscrontab -l
Remove all cron jobs for current usercrontab -r
Remove cron jobs with confirmationcrontab -i -r
Edit cron jobs for another usersudo crontab -u username -e
List cron jobs for another usersudo crontab -u username -l
Remove cron jobs for another usersudo crontab -u username -r
Install cron jobs from a filecrontab filename
Check cron service statussystemctl status cron
Run job every minute* * * * * command
Run job every hour0 * * * * command
Run job daily at midnight0 0 * * * command
Run job weekly (Sunday midnight)0 0 * * 0 command
Run job monthly0 0 1 * * command

This quick reference summarizes the most commonly used crontab command examples in Linux for scheduling and managing automated tasks.


How cron and crontab Work

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

bash
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.

bash
/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.

bash
MIN  HOUR  DOM  MON  DOW  COMMAND
FieldMeaningAllowed Values
MINMinute0–59
HOURHour0–23
DOMDay of Month1–31
MONMonth1–12 or jan–dec
DOWDay of Week0–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

bash
crontab -l

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

Edit current user’s cron jobs

bash
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

bash
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

bash
0 20 * * * myscript.sh

Runs daily at 8:00 PM.

Run a job multiple times per day

bash
0 7,21 * * * myscript.sh

Runs every day at 7 AM and 9 PM.

Run multiple commands in a single cron job

bash
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.

bash
@yearly    @monthly    @weekly    @daily    @hourly

Example:

bash
@daily myscript.sh

These are easier to read and reduce syntax mistakes.

Run a job once per day

bash
@daily backup.sh

Ideal for:

  • Daily backups
  • Log cleanup
  • Health checks

Equivalent to:

bash
0 0 * * *

Run a job once per week

bash
@weekly cleanup.sh

Commonly used for:

  • Disk cleanup
  • Weekly reports
  • Maintenance tasks

Equivalent to:

bash
0 0 * * 0

Run a job once per month

bash
@monthly billing.sh

Useful for:

  • Monthly reports
  • Billing or accounting scripts
  • Data archival

Equivalent to:

bash
0 0 1 * *

Run a job once per year

bash
@yearly archive.sh

Suitable for:

  • Annual archiving
  • License renewal checks
  • Yearly audits

Equivalent to:

bash
0 0 1 1 *

Run a job every hour

bash
@hourly sync.sh

Good for:

  • Data synchronization
  • Cache refresh jobs
  • Monitoring scripts

Equivalent to:

bash
0 * * * *

Run a job at system startup

bash
@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

bash
sudo crontab -u username -l

Lists all cron jobs configured for the specified user.

Edit another user’s crontab

bash
sudo crontab -u username -e

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

Remove another user’s crontab

bash
sudo crontab -u username -r

Prompt before removal:

bash
sudo crontab -u username -i -r

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

bash
* * * * * /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.