Cron job
A cron job is a single task registered for recurring, unattended execution by the cron daemon on Unix-like systems. Concretely, it is one line in a crontab: a five-field time pattern paired with a command — a shell script, a binary, or a curl call to a URL — that the system fires automatically whenever the pattern matches the clock.
How a cron job works
The schedule fields cover minute, hour, day of month, month and day of week. Some realistic entries:
- * * * * * php /var/www/app/artisan schedule:run — ticks a Laravel application scheduler every minute,
- 15 3 * * * pg_dump shop | gzip > /backups/shop.sql.gz — dumps a database daily at 03:15,
- 0 6 * * 1-5 /opt/bin/send-daily-digest.sh — sends a digest on weekday mornings.
Each job runs with the permissions of the crontab's owner and in a stripped-down environment without your interactive shell settings. The two habits that prevent most cron mysteries are using absolute paths everywhere and redirecting output to a log file (>> /var/log/job.log 2>&1) so failures leave evidence.
Practical applications
Cron jobs are the workhorses of server maintenance and web application operations: database and file backups, log rotation, cache warming, clearing temp directories, syncing data with external APIs, renewing TLS certificates, dispatching newsletters, and recomputing reports during off-peak hours. Many platforms expect one: replacing WordPress's traffic-triggered wp-cron with a real system job makes scheduled posts reliable, and frameworks like Laravel or Symfony hang their entire scheduling layer off a single crontab entry.
On shared hosting you typically create jobs through the control panel; on a VPS you manage the crontab directly over SSH. Regardless of environment, a few practices pay off:
- log every run and configure failure notifications (the MAILTO variable is the simplest option),
- lock long jobs against overlap with flock or an equivalent,
- schedule heavy work outside peak traffic windows,
- monitor execution externally — a heartbeat check that alerts when a job goes silent catches the failures your logs cannot report.
Powiązane pojęcia
Najczęstsze pytania
How often can a cron job run?
Standard cron resolves to one minute — that is the shortest native interval. If you need sub-minute execution, either run a loop inside a script started every minute, use systemd timers with second-level precision, or let an application-level scheduler handle the fine granularity.
What happens if a cron job is still running when the next run starts?
Cron happily starts a second instance, which can pile up processes and corrupt data. Guard long-running jobs with a lock, for example by wrapping the command in flock -n /tmp/job.lock, so overlapping executions exit immediately instead of stacking.
