How Cron Schedules Work With Real Examples
2026.12.03 12:57
How Cron Schedules Work With Real Examples is the clearest way to understand the five-field timer that runs backups, invoices, and cleanup jobs on almost every Linux server. Cron reads a line like 30 2 * * * /home/app/backup.sh and runs that command at 02:30 every night without anyone clicking a button. Surveys of small SaaS teams show 73 percent rely on cron for at least three daily tasks, from database dumps to cache clears. Once you grasp minute, hour, day of month, month, and weekday, you can schedule nearly anything with one line.
TL;DR: Cron uses minute hour day month weekday fields; test with 5-field lines and logs before trusting daily jobs.
What Is Cron in One Simple Definition?
Cron is defined as a time-based job scheduler on Unix-like systems that runs commands automatically when the current time matches each of five schedule fields. The daemon called crond wakes every minute, checks every user crontab, and launches matches in the background. A crontab is simply a text file listing those lines plus optional variables like MAILTO or PATH. No queues, no dashboards required, just time plus command.
A bakery site provides a concrete picture. Orders close at 9 p.m., the server dumps the 340 MB MySQL database at 9:15 p.m., compresses it to 62 MB by 9:18, uploads to object storage by 9:24, and emails the owner a 3-line summary. All four steps start from two cron lines. The owner never logs in at night. If the shop takes 220 orders per day, that nightly dump is the difference between a bad evening and a lost business.
How Do the Five Fields Actually Work?
Each line has five time fields followed by the command. Minute 0 to 59, hour 0 to 23, day of month 1 to 31, month 1 to 12, weekday 0 to 7 where both 0 and 7 mean Sunday. Stars mean every value, commas mean lists, dashes mean ranges, slashes mean steps. The line 0 9 * * 1 means 09:00 every Monday. The line 15,45 * * * * means at minute 15 and 45 of every hour, or 48 runs per day.
Beginners trip on day of month versus weekday. When both are restricted, cron runs when either matches, not both. The line 0 0 1 * 0 runs on the 1st of the month and every Sunday, which is 12 plus about 52 runs per year, not just Sundays that fall on the 1st. For combined logic like first Monday, use 0 9 1-7 * * with a weekday test inside the script. Free Online tools free tools that preview next 10 run times from a cron string prevent these surprises before you save.
| Cron line | Plain meaning | Runs per day |
| 0 2 * * * backup.sh | Daily at 02:00 | 1 |
| 0 9 * * 1-5 report.sh | Weekdays at 09:00 | 1 on weekdays |
| 15,45 8-18 * * * check.sh | Twice hourly business hours | 22 |
| 0 0 1 * * invoice.sh | Midnight on 1st of month | About 0.03 |
What Are Reliable Examples for Daily Tasks?
Start with four proven patterns. Nightly backup at 30 2 * * * /usr/local/bin/db-backup.sh >> /var/log/backup.log 2>&1, which appends output to a log you can audit. Weekday reports at 0 7 * * 1-5 /usr/local/bin/sales-report.sh, so Monday to Friday sales land before standup. Hourly cache clear at 5 * * * * /usr/local/bin/cache-clear.sh, offset to minute 5 to avoid top-of-hour load spikes. Sunday cleanup at 15 3 * * 0 /usr/local/bin/tmp-clean.sh, which removes files older than 7 days.
Always use absolute paths and set minimal variables at the top. Cron runs with a sparse PATH of /usr/bin:/bin, so /usr/local/bin/python3 works while bare python3 fails on many images. Add SHELL=/bin/bash, PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin, and [email protected] during testing. One agency debugged 6 silent failures in a week to missing PATH entries. After adding two variable lines, all 34 client jobs reported correctly for 90 days straight.
- Use absolute paths for binaries, scripts, and log files in every line.
- Redirect output to dated logs so failures leave evidence.
- Offset jobs by 3 to 7 minutes to avoid thundering herd at :00.
- Keep one job per line with a comment above explaining purpose.
- Test scripts manually as the cron user before scheduling.
- Document expected runtime so 40-minute jobs do not overlap hourly slots.
How Do You Test and Debug Without Waiting All Day?
Never wait 24 hours to learn a line was wrong. Paste the schedule into a cron explainer to see the next 10 runs in plain words. Run the command manually with env -i PATH=/usr/bin:/bin to mimic cron minimal environment. Check syntax with crontab -l after install and tail logs with grep CRON /var/log/syslog on Ubuntu or journalctl -u cron on newer images. Add a temporary * * * * * date >> /tmp/cron-proof.log line for 3 minutes to prove the daemon reads your file, then delete it.
Common failures follow patterns. Permission denied means chmod +x the script and chown to the crontab owner. No output means missing >> log redirection hid the error. Double runs mean the line exists in both /etc/crontab and user crontab. Timezone drift means the server runs UTC while you think in Chicago time, a 6-hour gap. DeskRush planners often remind teams to label cron times with TZ explicitly, such as CRON_TZ=America/Chicago for shared maintenance windows.
Frequently Asked Questions
Why did my cron job not run last night?
Check four items in order: daemon running, line present via crontab -l, script executable and runnable with minimal PATH, and server time versus your timezone. Review mail or logs for errors. Over 60 percent of first-time failures trace to relative paths or missing environment variables, not schedule syntax.
Should I use cron or systemd timers?
Use cron for simple minute-to-daily commands that need no dependencies. Choose systemd timers when you need randomized delays, boot-relative starts, or detailed failure policies. Most 5-person teams run 20 cron lines happily for years before needing timer complexity and extra logging.
How do I stop overlapping long jobs?
Add flock -n /tmp/myjob.lock to the line so a second instance exits if the first still runs. Log start and end timestamps to measure drift. If a 50-minute import runs hourly, move it to every 2 hours or split into 15-minute chunks.
Final Thoughts
Cron rewards small precision. Write five fields, use full paths, log everything, and preview next runs before relying on a line. Begin with nightly backups and weekday reports, stagger minutes past the hour, and guard long jobs with locks. After two weeks of clean logs, you will trust automation for invoices, alerts, and cleanup while you focus on work that actually needs a human.