Understanding Cron Jobs

What is a Cron Job?

A cron job is your friendly automated task scheduler. Think of it as your personal assistant that runs commands or scripts at specific times - whether it's every hour, daily, weekly, or on any schedule you choose.

How Does It Work?

Cron uses a simple yet powerful format with five time fields: minute, hour, day of month, month, and day of week. Each field can contain specific values, ranges, or special characters to create flexible scheduling patterns.

Managing Scheduled Tasks

On macOS & Linux

View your cron jobs:

crontab -l

Edit your cron jobs:

crontab -e

View system-wide cron jobs (requires sudo):

sudo cat /etc/crontab

On Windows

Windows uses Task Scheduler instead of cron. You can:

  • Open Task Scheduler by pressing Win + R, typing taskschd.msc, and pressing Enter
  • Use PowerShell to manage scheduled tasks:

View all scheduled tasks:

Get-ScheduledTask

Create a new scheduled task (basic example):

$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "path\to\script.ps1" $trigger = New-ScheduledTaskTrigger -Daily -At 2am Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "DailyBackup" -Description "Daily backup task"

Common Use Cases

  • Database backups at midnight
  • Sending daily report emails
  • Cleaning up temporary files weekly
  • Updating system packages monthly
  • Running health checks every 5 minutes

Special Characters

Basic

  • * - Run every time unit
  • , - List of values
  • - - Range of values
  • / - Step values

Advanced

  • L - Last day of month/week
  • W - Nearest weekday
  • # - Nth day of month
  • ? - No specific value

Example Cron Jobs

Run a backup every day at 2 AM:

0 2 * * * /path/to/backup.sh

Run a script every 15 minutes:

*/15 * * * * /path/to/script.sh

Run a task every weekday at 9 AM:

0 9 * * 1-5 /path/to/task.sh

References & Resources

Official Documentation

Tutorials & Guides

Windows Resources

Note: External links will open in a new tab. We are not affiliated with any of the linked resources.