What Is a Cron Job? Scheduled Tasks Explained
A cron job is a scheduled task that runs automatically at specified times on Unix-based systems. Learn what cron jobs are, how the syntax works, and how to schedule tasks on Linux, macOS, and in the cloud.
Cron Jobs Explained Simply
A cron job is a command or script that the cron daemon runs automatically on a schedule you define. The schedule is specified using a 5-field expression that controls the minute, hour, day, month, and weekday the job runs.
- The cron daemon: A background process (
crond) that wakes up every minute, checks the crontab, and runs any scheduled jobs - crontab: The configuration file listing jobs and their schedules โ edit with
crontab -e - Expression format:
minute hour day month weekday command - Runs silently: Output is emailed to the user unless redirected โ use
>> /var/log/myjob.log 2>&1to capture it
Cron Expression Syntax
- Five fields:
* * * * *= every minute. Fields left to right: minute (0โ59), hour (0โ23), day-of-month (1โ31), month (1โ12), day-of-week (0โ7, 0 and 7 both = Sunday) *= any value:0 9 * * *= 9:00 AM every day*/n= every n units:*/15 * * * *= every 15 minutesa-b= range:0 9 * * 1-5= 9 AM weekdays onlya,b,c= list:0 9,17 * * *= 9 AM and 5 PM daily- Common examples:
0 0 * * *= midnight daily;0 0 1 * *= 1st of each month;0 0 * * 0= every Sunday midnight
Common Uses for Cron Jobs
- Database backups:
0 2 * * *โ run a backup script at 2 AM daily, before business hours - Log rotation:
0 0 * * 0โ compress and archive log files weekly - Report generation:
0 8 * * 1โ email a weekly report every Monday at 8 AM - Cache warming:
*/5 * * * *โ refresh cached data every 5 minutes - SSL certificate renewal:
0 12 * * *โ Let's Encrypt's certbot runs daily to check and renew certificates - Data sync:
0 */4 * * *โ sync data from an external API every 4 hours
Frequently Asked Questions
How do I create a cron job on Linux or macOS?
Run crontab -e in your terminal to open the crontab editor (uses $EDITOR, typically nano or vi). Each line is one job in the format schedule command. Save and exit โ the cron daemon picks up changes immediately. View your current crontab with crontab -l. Remove all your cron jobs with crontab -r (be careful โ this deletes all jobs without confirmation). System-wide cron jobs live in /etc/cron.d/ and /etc/crontab.
What timezone does cron use?
By default, cron runs in the system's local timezone. To use a different timezone for a specific crontab, add TZ=America/New_York (or any IANA timezone name) as a line before the jobs that should use it. Cloud schedulers like AWS EventBridge and GitHub Actions run in UTC by default โ always specify times in UTC or use a timezone-aware scheduler. A common mistake is setting 0 9 * * * intending 9 AM local time, only to find jobs run at the wrong time on a UTC server.
Why isn't my cron job running?
The most common causes: (1) Wrong path โ cron runs with a minimal PATH; use absolute paths for commands (/usr/bin/python3 not python3). (2) No output captured โ cron silently discards output by default; add >> /tmp/myjob.log 2>&1 to see errors. (3) Permission issue โ the script isn't executable (chmod +x script.sh). (4) Syntax error โ verify your expression with a cron expression generator before deploying. (5) cron daemon not running โ check with systemctl status cron or service cron status.