Cron Job Examples: A Practical Crontab Cheat Sheet

On this pageCron Job Examples: A Practical Crontab Cheat Sheet0%
- Cron Job Examples: A Practical Crontab Cheat Sheet
- Copy-ready cron schedules
- How the five fields work
- Shortcuts that save typing
- Five gotchas that break cron schedules
- 1. Day of month and day of week combine with OR
- 2. The percent sign is special
- 3. Steps reset inside each field
- 4. Cron runs with a minimal environment
- 5. Time zones depend on the machine
- Keep scheduled jobs safe
- Installing and checking a crontab
- FAQ
- How do I run a cron job every 5 minutes?
- How do I run a cron job every hour?
- Is Sunday 0 or 7 in cron?
- Why does my script work in the terminal but not in cron?
- Can cron run something every 90 minutes?
- Where cron fits in a bigger automation stack
- Related reading
Cron Job Examples: A Practical Crontab Cheat Sheet
A cron schedule has five fields: minute, hour, day of month, month, and day of week. Write the fields, then the command. For example, */5 * * * * /home/me/bin/sync.sh runs a script every five minutes. The examples below are copy-ready for the cron daemons found on Linux and macOS, with the syntax rules taken from the crontab(5) manual.
Copy-ready cron schedules
Every minute: * * * * *
Every 5 minutes: */5 * * * *
Every 15 minutes: */15 * * * *
Every 30 minutes: */30 * * * *
Every hour, on the hour: 0 * * * *
Every 2 hours: 0 */2 * * *
Every 6 hours: 0 */6 * * *
Every day at 06:30: 30 6 * * *
Weekdays at 09:00: 0 9 * * 1-5
Every Monday at 08:00: 0 8 * * 1
First day of every month at midnight: 0 0 1 * *
Every 10 minutes during work hours, weekdays: */10 9-17 * * 1-5
A full crontab line puts the command after the five fields:
# m h dom mon dow command
*/5 * * * * /home/me/bin/sync.sh >> /home/me/logs/sync.log 2>&1How the five fields work
The manual lists the allowed values for each field:
Minute: 0–59
Hour: 0–23
Day of month: 1–31
Month: 1–12, or three-letter names such as jan
Day of week: 0–7, where both 0 and 7 mean Sunday, or names such as mon
Four operators build every schedule in the list above:
An asterisk (*) means every value in the field, from first to last.
A hyphen (-) defines an inclusive range. 8-11 in the hour field runs at 8, 9, 10, and 11.
A comma (,) builds a list. 1,15 in the day-of-month field runs on the 1st and the 15th.
A slash (/) adds a step. */2 in the hour field runs every second hour, and 0-23/2 means the same thing.
Shortcuts that save typing
The cronie implementation documented in crontab(5) accepts these nicknames in place of the five fields:
@reboot runs once after the machine starts.
@hourly equals 0 * * * *.
@daily equals 0 0 * * *.
@weekly equals 0 0 * * 0.
@monthly equals 0 0 1 * *.
@yearly equals 0 0 1 1 *.
@reboot is handy for starting a small watcher or a personal dashboard job. If you want a worked example of a scheduled job feeding a page you check each morning, see build a self-updating personal dashboard with cron.
Five gotchas that break cron schedules
1. Day of month and day of week combine with OR
When both day fields are restricted, cron runs the job when either one matches. The manual gives 30 4 1,15 * 5 as an example: it runs at 4:30 on the 1st and 15th of each month, and also on every Friday. If you want "the first Monday of the month", cron syntax alone cannot express it. Schedule the job for Mondays and let the script exit unless the date is 7 or lower:
0 9 * * 1 [ "$(date +\%d)" -le 07 ] && /home/me/bin/monthly-report.sh2. The percent sign is special
In the command part of a crontab line, an unescaped % becomes a newline, and everything after the first % is sent to the command as standard input. Escape it as \%, which is why the date call above uses \%d.
3. Steps reset inside each field
A step only counts within its own field. The manual notes that */23 in the hour field runs at hour 0 and hour 23 of each day, not every 23 hours. For intervals that do not divide evenly into an hour or a day, schedule the job on a shorter interval and let the script decide whether to act.
4. Cron runs with a minimal environment
Cron starts commands with /bin/sh and only a few variables set, such as HOME, LOGNAME, and SHELL. Your interactive aliases and PATH additions are not there. Use absolute paths for scripts and binaries, or set the variables at the top of the crontab:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""An empty MAILTO="" stops cron from mailing job output. Redirect output to a log file instead so failures leave evidence.
5. Time zones depend on the machine
Cron uses the system clock. Cronie also supports a CRON_TZ variable for writing a table in another zone, but other cron implementations may not, so check your system's man page before relying on it. On a laptop or VPS set to UTC, 0 6 * * * runs at 06:00 UTC, not 06:00 in your city.
Keep scheduled jobs safe
Three habits prevent expensive failures:
Log every run. Append stdout and stderr to a file with >> file.log 2>&1, and include a timestamp in your script's first line of output.
Prevent overlapping runs. If a job can run longer than its interval, wrap it with flock from util-linux so a second copy exits instead of piling up: */5 * * * * flock -n /tmp/sync.lock /home/me/bin/sync.sh. The -n option makes flock fail instead of waiting when the lock is held (flock(1) manual).
Make the script safe to repeat. A job that runs twice after a reboot or a manual test should not send two reports or write duplicate rows. The same idea appears in small guards for reliable AI workflows.
Installing and checking a crontab
crontab -e opens your user crontab in an editor.
crontab -l prints the current entries.
On systems with cronie, crontab -T file tests a crontab file's syntax before you install it, as the crontab(5) page notes.
After saving, wait for the next scheduled minute and read the log. If nothing appears, test the exact command from a shell started with a clean environment, such as env -i /bin/sh -c '/home/me/bin/sync.sh', which exposes missing PATH entries quickly.
FAQ
How do I run a cron job every 5 minutes?
Use */5 * * * * followed by the absolute path to your command.
How do I run a cron job every hour?
Use 0 * * * * or the @hourly shortcut.
Is Sunday 0 or 7 in cron?
Both. The day-of-week field accepts 0 and 7 for Sunday.
Why does my script work in the terminal but not in cron?
Cron runs with a minimal environment. Use absolute paths, set PATH in the crontab, and redirect output to a log to see the error.
Can cron run something every 90 minutes?
Not with a single expression. Run the job every 30 minutes and let the script check the time since its last successful run.
Where cron fits in a bigger automation stack
Cron is a good fit for one machine and a handful of jobs. Once scheduled scripts start calling several APIs, sending notifications, and needing retries, the scheduling is no longer the hard part. Shell script automation covers the scripting side, and when the job graph grows beyond what one crontab can track, tim software house yang biasa membangun otomatisasi kerja dengan AI can help map the process before you pick a heavier scheduler.