Nodea — logo

Cron

Cron is the classic time-based job scheduler of Unix-like operating systems — a daemon that runs continuously in the background and executes commands or scripts at times you define. The name derives from the Greek chronos (time). Present on virtually every Linux server, cron is the default answer whenever something needs to happen automatically and repeatedly without a human at the keyboard.

How cron works

The daemon wakes once a minute and scans its schedule sources: per-user crontab files, the system-wide /etc/crontab, and drop-in directories such as /etc/cron.d or /etc/cron.daily. Any entry whose time specification matches the current minute gets executed. Each schedule line uses five time fields followed by the command:

minute hour day-of-month month day-of-week command

For example, 0 2 * * 0 /opt/scripts/db-backup.sh fires every Sunday at 02:00. Asterisks mean "any value", slashes define steps (*/10 in the minute field means every ten minutes), and commas or ranges build lists. Shortcuts like @daily, @weekly and @reboot cover the common cases without arithmetic.

One crucial detail: cron executes jobs in a bare-bones environment with a minimal PATH and none of your interactive shell configuration. Scripts that run fine by hand routinely fail under cron until every path is made absolute and required variables are set explicitly.

Practical applications

On a typical web server, cron quietly handles the plumbing:

  • nightly database dumps and file backups,
  • log rotation and cleanup of temporary files,
  • SSL certificate renewal (Let's Encrypt's certbot relies on it),
  • queue processing and scheduled emails,
  • application schedulers — WordPress wp-cron replacement, Laravel's schedule:run, Magento indexers.

On shared hosting, cron jobs are usually managed through the control panel (cPanel, DirectAdmin, Plesk); on a VPS you edit schedules yourself over SSH. Whichever way you run it, log each job's output and monitor that it actually executes — a silently failing backup job is the kind of problem you only discover on the worst possible day.

Powiązane pojęcia

Najczęstsze pytania

Why does my script work in the terminal but fail under cron?

Cron runs jobs in a minimal environment: no interactive shell profile, a stripped-down PATH and no session variables. Use absolute paths for every binary and file, set required environment variables inside the script or crontab, and redirect output to a log file to see the actual error.