What is a Cron ?

A cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule tasks (referred to as “cron jobs”) to run automatically at specified times or intervals.

Key Features of Cron

  1. Automation: Cron is widely used for automating repetitive tasks such as backups, updates, sending emails, or system maintenance.
  2. Flexibility: Jobs can be scheduled to run at specific times, dates, or intervals (e.g., daily, weekly, every minute).
  3. Cron Syntax: Jobs are defined in a file called a crontab (cron table), where users specify the timing and the command to run.

Cron Syntax

A typical cron job entry in the crontab file follows this structure:

* * * * * command_to_execute
- - - - -
| | | | |  
| | | | +---- Day of the week (0 - 7) [Both 0 and 7 represent Sunday]  
| | | +------ Month (1 - 12)  
| | +-------- Day of the month (1 - 31)  
| +---------- Hour (0 - 23)  
+------------ Minute (0 - 59)

Example Cron Jobs

  1. Run a script every day at midnight: 0 0 * * * /path/to/script.sh
  2. Run a command every hour: 0 * * * * /usr/bin/python3 /path/to/script.py
  3. Run a task every minute: * * * * * echo "Hello, World!"
  4. Run a task on the 1st of each month at 3:30 AM: 30 3 1 * * /path/to/monthly_task.sh

Managing Cron Jobs

  • Edit Crontab: Use crontab -e to open and edit the cron table.
  • List Cron Jobs: Use crontab -l to view all cron jobs for the current user.
  • Remove Crontab: Use crontab -r to remove all cron jobs for the current user.

Cron is a powerful tool for managing automated tasks and is essential for system administrators and developers alike.