Linux cronjobs are essential for automating repetitive tasks on servers and workstations. Whether you're scheduling database backups, sending automated reports, or running maintenance scripts, understanding cron expression syntax is a must-have skill for developers and system administrators.
This guide covers everything you need to know about Linux cronjobs from basic cron syntax to advanced cron job examples you can use immediately.
What is a Cron Expression?
A cron expression is a string that defines when a scheduled task (cronjob) should run. The Linux cron daemon reads these expressions from your crontab file and executes commands at the specified times.
Every cron expression consists of five fields separated by spaces, followed by the command to execute:
* * * * * /path/to/command
The Five-Field Cron Format
* * * * *
│ │ │ │ │
│ │ │ │ └─ Day of week (0-6, Sunday=0)
│ │ │ └─── Month (1-12)
│ │ └───── Day of month (1-31)
│ └─────── Hour (0-23)
└───────── Minute (0-59)
Each asterisk (*) acts as a wildcard meaning "every." Replace asterisks with specific values or patterns to define your schedule.
Essential Cron Commands
Before diving into cron job examples, here are the cron commands you'll use daily:
| Command | Description |
|---|---|
crontab -e |
Edit your crontab file |
crontab -l |
List all your cron jobs |
crontab -r |
Remove all your cron jobs |
crontab -u user -e |
Edit another user's crontab (root only) |
To create your first Linux cronjob, run:
crontab -e
This opens your crontab file in the default editor. Add your cron expression and command, save, and the cron daemon automatically picks up the changes.
Basic Cron Job Examples
Run Every Minute
* * * * * /usr/bin/php /var/www/check-queue.php
The simplest cron expression runs the command every single minute.
Run Every 5 Minutes
*/5 * * * * /home/user/scripts/health-check.sh
The */5 means "every 5 minutes" (at minutes 0, 5, 10, 15, etc.).
Run Every Hour
0 * * * * /usr/bin/python3 /opt/scripts/hourly-report.py
Runs at minute 0 of every hour (1:00, 2:00, 3:00, etc.).
Run Daily at Midnight
0 0 * * * /usr/local/bin/backup-database.sh
Executes once per day at 00:00 (midnight).
Run Daily at 9 AM
0 9 * * * /home/user/scripts/morning-report.sh
Runs at 9:00 AM every day.
Run Every Monday at 9 AM
0 9 * * 1 /opt/scripts/weekly-digest.sh
Runs at 9:00 AM every Monday. In cron, Monday = 1.
Special Characters in Cron Expressions
Linux cron supports four special characters for flexible scheduling:
Asterisk (*)
Matches all values. Use when you want the job to run for every possible value of that field.
Comma (,)
Separates multiple values. Example: 1,15 in the day-of-month field means "on the 1st and 15th."
0 9 1,15 * * /opt/scripts/payroll.sh
Hyphen (-)
Defines a range. Example: 9-17 in the hour field means "from 9 AM to 5 PM."
0 9-17 * * * /opt/scripts/business-hours-check.sh
Slash (/)
Specifies intervals. Example: */15 means "every 15 units."
*/15 * * * * /opt/scripts/check-status.sh
Real-World Cron Job Examples
Business Hours Only (Weekdays, 9 AM - 5 PM)
0 9-17 * * 1-5 /opt/scripts/office-hours-task.sh
Runs every hour from 9 AM to 5 PM, Monday through Friday.
Twice Daily (9 AM and 6 PM)
0 9,18 * * * /opt/scripts/sync-data.sh
First Day of Every Month
0 0 1 * * /opt/scripts/monthly-report.sh
Runs at midnight on the 1st of each month.
Quarterly Reports
0 0 1 1,4,7,10 * /opt/scripts/quarterly-report.sh
Runs at midnight on the first day of January, April, July, and October.
Every 30 Minutes During Business Hours
*/30 9-17 * * 1-5 /opt/scripts/frequent-check.sh
Linux Cronjob Best Practices
1. Always Use Absolute Paths
Cron runs with a minimal PATH environment. Always specify full paths:
Bad:
0 0 * * * python backup.py
Good:
0 0 * * * /usr/bin/python3 /home/user/scripts/backup.py
2. Redirect Output to Log Files
By default, cron sends output via email. Redirect to log files instead:
0 0 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1
The 2>&1 redirects both stdout and stderr to the same file.
3. Set Environment Variables
Define important variables at the top of your crontab:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=admin@example.com
0 0 * * * /opt/scripts/nightly-job.sh
4. Avoid Scheduling at Midnight
Everyone schedules cron jobs at midnight. Spread the load:
# Instead of midnight, run at 3:17 AM
17 3 * * * /opt/scripts/heavy-job.sh
5. Test Your Expressions
Use our Cron Generator to validate expressions and see upcoming execution times before adding to your crontab.
Common Cron Mistakes to Avoid
- Wrong day-of-week numbering: Sunday = 0 (or 7), Monday = 1
- Forgetting months are 1-12: Not 0-11 like some programming languages
- PATH issues: Commands that work in terminal may fail in cron
- Not accounting for long-running jobs: Add lock files or use
flock - Timezone confusion: Cron uses the server's timezone, not UTC
Where Are Cron Jobs Stored on Linux?
| Location | Purpose |
|---|---|
/var/spool/cron/crontabs/ |
User crontabs (Debian/Ubuntu) |
/var/spool/cron/ |
User crontabs (RHEL/CentOS) |
/etc/crontab |
System-wide crontab |
/etc/cron.d/ |
Additional system cron jobs |
/etc/cron.hourly/ |
Scripts that run hourly |
/etc/cron.daily/ |
Scripts that run daily |
/etc/cron.weekly/ |
Scripts that run weekly |
/etc/cron.monthly/ |
Scripts that run monthly |
Checking Cron Logs
To verify your Linux cronjobs are running:
Ubuntu/Debian:
grep CRON /var/log/syslog
RHEL/CentOS/Fedora:
journalctl -u crond
Conclusion
Linux cronjobs are powerful tools for automating any recurring task. With the five-field cron expression format and the special characters covered in this guide, you can create precise schedules for virtually any use case.
Key takeaways:
- Use
crontab -eto edit andcrontab -lto list your cron jobs - Always use absolute paths in your cron commands
- Redirect output to log files for debugging
- Test expressions before deploying to production
Ready to create your own Linux cronjobs? Try our Cron Generator to build and validate cron expressions with an interactive tool!