How Cron Works

Understand the internal process and architecture of cron daemon.

Quick Summary: Cron daemon runs continuously, checking every minute if any scheduled jobs should execute based on their time expressions and system time.

Cron Architecture

Core Components:

1

Cron Daemon (crond)

Background process that manages all cron jobs

2

Crontab Files

Files containing scheduled jobs and their time expressions

3

Job Scheduler

Logic that determines when jobs should run

4

Job Executor

Runs the actual commands and handles output

Execution Flow

🔄 Step 1: Daemon Startup

When the system boots, the cron daemon starts automatically:

  • Reads configuration from /etc/crontab
  • Scans /etc/cron.d/ directory
  • Loads user crontabs from /var/spool/cron/
  • Sets up signal handlers and logging

⏰ Step 2: Time Checking Loop

Cron enters an infinite loop that runs every minute:

while (true) { current_time = get_current_time(); check_all_jobs(current_time); sleep_until_next_minute(); }

📋 Step 3: Job Evaluation

For each job, cron checks if current time matches the schedule:

  • Parse cron expression into 5 fields
  • Compare each field with current time
  • All fields must match for job to run
  • Handle special cases (*, ranges, steps)

🚀 Step 4: Job Execution

When a job matches, cron executes it:

  • Fork a new process
  • Set environment variables
  • Change to user's home directory
  • Execute the command via shell
  • Capture output and errors

Timing Precision

Important Timing Facts

  • ✓ Cron checks jobs every minute (not every second)
  • ✓ Jobs scheduled for the same minute may run in any order
  • ✓ System time changes can affect scheduling
  • ✓ Heavy system load can delay job execution

File Locations

File/Directory Purpose Access
/etc/crontab System-wide crontab Root only
/etc/cron.d/ System cron jobs Root only
/var/spool/cron/ User crontabs Per user
/var/log/cron Cron execution logs Read-only

Next Steps

Now that you understand how cron works internally, learn how to install and configure it: