Skip to content

Cron job scheduling

Cron job scheduling is a time-based job scheduler used in Unix-like computer operating systems. It enables users to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals^[600-developer__tools__cron_cheat_sheet.md].

Overview

The cron daemon is a background process that executes scheduled tasks defined in crontabs (cron tables). These tables contain instructions for the cron daemon to run specific jobs at specific times^[600-developer__tools__cron_cheat_sheet.md]. This mechanism is essential for automating system maintenance tasks, such as log rotation, backups, or [[筆記法|recording workflows]]^[600-developer__tools__cron_cheat_sheet.md].

Crontab Syntax

Cron jobs are defined using a specific syntax comprising five fields for time, followed by the command to be executed^[600-developer__tools__cron_cheat_sheet.md]. The syntax typically follows this order:

[Minute] [Hour] [Day_of_Month] [Month] [Day_of_Week] [Command]

Field Definitions

  • Minute: 0–59^[600-developer__tools__cron_cheat_sheet.md]
  • Hour: 0–23^[600-developer__tools__cron_cheat_sheet.md]
  • Day of Month: 1–31^[600-developer__tools__cron_cheat_sheet.md]
  • Month: 1–12^[600-developer__tools__cron_cheat_sheet.md]
  • Day of Week: 0–7 (where both 0 and 7 represent Sunday)^[600-developer__tools__cron_cheat_sheet.md]

Special Characters

Special characters are used to specify repetition or ranges:

  • Asterisk (*): Represents "all" or "every" possible value (e.g., every minute or every hour)^[600-developer__tools__cron_cheat_sheet.md].
  • Comma (,): Separates list values (e.g., 1,3,5).
  • Hyphen (-): Defines a range (e.g., 1-5).
  • Forward slash (/): Defines step values (e.g., */5 means "every 5 units").

Examples

  • * * * * *: Runs every minute^[600-developer__tools__cron_cheat_sheet.md].
  • 5 4 * * *: Runs at 04:05 every day^[600-developer__tools__cron_cheat_sheet.md].
  • */15 * * * *: Runs every 15 minutes^[600-developer__tools__cron_cheat_sheet.md].
  • 0 */2 * * *: Runs every 2 hours^[600-developer__tools__cron_cheat_sheet.md].

Configuration Files

Cron configurations can be managed globally for the system or specifically for individual users^[600-developer__tools__cron_cheat_sheet.md].

System-wide Crontabs

  • /etc/crontab: The main system crontab file^[600-developer__tools__cron_cheat_sheet.md].
  • /etc/cron.d/: A directory containing additional system-wide cron files^[600-developer__tools__cron_cheat_sheet.md].

User Crontabs

Individual users can have their own crontab files. These are typically stored and managed using the crontab command, which edits files located in a spool directory (often /var/spool/cron or /var/spool/cron/crontabs), rather than editing them directly^[600-developer__tools__cron_cheat_sheet.md].

Common Shortcuts

Linux distributions often provide predefined shortcuts for common scheduling intervals, such as: * @hourly * @daily * @weekly * @monthly * @reboot

These shortcuts simplify the syntax for standard intervals^[600-developer__tools__cron_cheat_sheet.md].

Sources