LinuxCommandLibrary

cron

Schedule commands to run automatically

TLDR

View documentation for managing cron entries

$ tldr crontab
copy

SYNOPSIS

The cron system itself runs as a daemon and is typically managed by the system's init process. Users interact with the cron system primarily through the crontab command to manage their scheduled jobs.

crontab [-u user] file
crontab [-u user] {-l | -r | -e | -i}

PARAMETERS

-u user
    Specifies the user whose crontab is to be edited or viewed. If omitted, the current user's crontab is used.

file
    Installs the specified file as the new crontab for the current user (or the user specified with -u).

-l
    Lists the current crontab content to standard output.

-r
    Removes the current crontab.

-e
    Edits the current crontab. This typically opens the crontab in the user's default text editor (e.g., VI or NANO).

-i
    Used with -r (e.g., crontab -ri) to prompt the user for confirmation before removing the crontab.

DESCRIPTION

The cron utility is a powerful time-based job scheduler in Unix-like operating systems, including Linux. It allows users to schedule commands or scripts to run automatically at specified intervals. The core of the system is the cron daemon, which runs continuously in the background and checks for scheduled jobs. These jobs are defined in special files called crontab files (short for 'cron table').

While cron refers to the background service, user interaction for scheduling tasks is typically done via the crontab command. Each user can have their own crontab file, and there is also a system-wide crontab (often located in /etc/crontab or /etc/cron.d/) for system-level tasks. cron ensures that tasks like backups, log rotation, system updates, and custom script execution are performed regularly without manual intervention, making it a fundamental tool for system administration and automation.

CAVEATS

  • Environment Variables: cron jobs run in a minimal environment. PATH, HOME, and other variables might not be what you expect. Always use full paths to commands and scripts (e.g., /usr/bin/php instead of php).
  • Output and Mail: Any output (stdout or stderr) from a cron job is mailed to the user who owns the crontab, unless redirected. This can lead to mailbox clutter if not managed.
  • Permissions: Users must have appropriate permissions to run commands. The cron daemon runs with specific user permissions.
  • Syntax Strictness: The crontab file syntax is very specific (minute hour day-of-month month day-of-week command). Errors in syntax can prevent jobs from running.
  • System-wide vs. User Crontabs: Be aware of the distinction. System-wide crontabs (e.g., in /etc/crontab or /etc/cron.d/) often include a 'user' field that user crontabs do not.
  • Time Zones: cron jobs typically run based on the system's local time zone settings.

CRONTAB FILE FORMAT

Each line in a crontab file represents a single scheduled job and follows a specific format consisting of six fields:
minute hour day-of-month month day-of-week command-to-execute

  • Minute (0-59)
  • Hour (0-23)
  • Day of Month (1-31)
  • Month (1-12 or Jan-Dec)
  • Day of Week (0-7 or Sun-Sat, 0 and 7 are Sunday)
  • Command to Execute: The full command or script to run, including arguments.

Special characters like * (any value), , (list), - (range), and / (step) can be used. For example, *\/15 in the minute field means 'every 15 minutes'.
System-wide crontabs (e.g., /etc/crontab) often include an additional field after the day-of-week for the username under which the command should be run.

SPECIAL STRINGS FOR SCHEDULING

For common scheduling needs, cron supports several special strings that replace the first five time and date fields:

  • @reboot: Run once after every reboot.
  • @yearly or @annually: Run once a year (0 0 1 1 *).
  • @monthly: Run once a month (0 0 1 * *).
  • @weekly: Run once a week (0 0 * * 0).
  • @daily or @midnight: Run once a day (0 0 * * *).
  • @hourly: Run once an hour (0 * * * *).

HISTORY

The concept of scheduled tasks in Unix-like systems dates back to the early days of computing. The name 'cron' is derived from the Greek word 'chronos', meaning time. The original cron daemon was developed by Brian Kernighan at Bell Labs in the late 1970s. Its fundamental design, including the use of crontab files, has remained remarkably consistent over decades, evolving to include features like per-user crontabs and robust error handling. Modern implementations like Vixie cron (developed by Paul Vixie) and ISC cron are widely used, offering improved security and flexibility while maintaining backward compatibility with the classic cron syntax.

SEE ALSO

crontab(1), at(1), anacron(8), systemd.timer(5)

Copied to clipboard