Linux - Cron Jobs (crontab)

A cron job is a scheduled task in Linux/Unix systems that runs automatically at specific intervals. It is one of the most powerful automation tools used by system administrators, DevOps engineers, and developers to perform repetitive jobs without manual intervention. These tasks can run every minute, hour, day, week, month, or at custom times.

Cron jobs are managed using a file called crontab (short for “cron table”), which stores the schedule and command for each task.


1. What Is Cron?

Cron is a time-based job scheduler built into Linux. It constantly runs in the background and checks if any scheduled tasks need to be executed.

Whenever the time matches a job in the crontab file, cron runs that command automatically.


2. What Is Crontab?

Crontab is a configuration file that holds a list of scheduled cron jobs for each user.

There are two main commands:

  • crontab -e → Edit the cron jobs

  • crontab -l → List the cron jobs

Each user in the system can have their own crontab file.


3. Crontab Time Format

A cron job follows a 5-field format, which defines exactly when the task should run.

*    *    *    *    *   command
|    |    |    |    |
|    |    |    |    └── Day of the week (0–6: Sun=0)
|    |    |    └────── Month (1–12)
|    |    └────────── Day of the month (1–31)
|    └────────────── Hour (0–23)
└──────────────────── Minute (0–59)

Symbols:

  • * = every value

  • , = multiple values (e.g., 1,3,5)

  • - = range (e.g., 1-5)

  • / = step (e.g., */10 → every 10 minutes)


4. Examples of Cron Jobs

a) Run a script every day at 2:30 AM

30 2 * * * /home/user/backup.sh

b) Run a command every minute

* * * * * echo "Running every minute" >> /tmp/log.txt

c) Run a script every Monday at 5 PM

0 17 * * 1 /home/user/report.sh

d) Run a task every 10 minutes

*/10 * * * * /home/user/cleanup.sh

e) Run a task at system reboot

@reboot /home/user/startup.sh

5. Viewing and Editing Cron Jobs

To edit cron jobs:

crontab -e

This opens your default editor (like nano or vim).

To list cron jobs:

crontab -l

To remove all cron jobs:

crontab -r

6. Why Cron Jobs Are Important?

Cron jobs help automate tasks such as:

  • System backups

  • Log rotation

  • Cleaning temporary files

  • Running scripts at fixed times

  • Monitoring services

  • Updating databases

  • Sending scheduled reports

They improve efficiency, reduce human error, and keep systems running smoothly—even when you’re not around.


Final Summary

Cron jobs make Linux automation powerful and reliable. Using crontab, you can schedule anything—from simple commands to complex scripts—to run at exact times. Once set, cron works quietly in the background, saving time and ensuring consistent system operations.

 

If you're learning Linux automation, mastering crontab is a must!