Linux Cron Jobs
Learn how to schedule recurring tasks with cron, understand crontab syntax, and compare cron with systemd timers.
What Cron Is
cron is the traditional Linux scheduler for recurring tasks. It runs commands or scripts automatically at specific times, which makes it useful for backups, cleanup jobs, report generation, and health checks.
If you have ever needed something to run every night at 2:00 AM or every five minutes, cron is one of the first tools to learn.
Managing Your Crontab
Each user can have a personal crontab file.
Open it for editing:
crontab -e
List current entries:
crontab -l
Remove the current user crontab:
crontab -r
Be careful with crontab -r because it deletes all scheduled entries for that user.
Tip: Always run
crontab -lbefore making major changes so you know exactly what is already scheduled.
Understanding Cron Syntax
A cron schedule has five time fields followed by the command:
* * * * * /path/to/command
The fields are:
- minute
- hour
- day of month
- month
- day of week
Examples:
0 2 * * * /opt/scripts/backup.sh
*/5 * * * * /opt/scripts/health-check.sh
30 6 * * 1 /opt/scripts/weekly-report.sh
These mean:
0 2 * * *= every day at 02:00*/5 * * * *= every five minutes30 6 * * 1= every Monday at 06:30
Common Cron Job Examples
Backup script
Run a backup every night:
0 1 * * * /usr/local/bin/backup-db.sh >> /var/log/backup-db.log 2>&1
This appends both standard output and errors to a log file.
Log cleanup or rotation helper
15 3 * * 0 /usr/local/bin/archive-old-logs.sh
Health check
*/10 * * * * curl -fsS http://localhost:8080/health || echo "health check failed"
A health check job can test an internal endpoint and alert if the result is unexpected.
Note: Cron runs with a limited environment. Commands that work in your interactive shell may fail unless you use full paths or set needed variables explicitly.
System Cron Directories
Not all scheduled jobs live in a user crontab. Linux systems also include system-wide scheduling locations.
/etc/cron.d/
This directory contains cron files managed by packages or administrators. Files here usually include an extra field for the user account that should run the job.
Example entry:
0 4 * * * root /usr/local/bin/system-backup.sh
/etc/cron.daily/, /etc/cron.weekly/, and friends
These directories hold scripts that run on a daily, weekly, or monthly schedule depending on the system configuration.
You may see:
/etc/cron.hourly//etc/cron.daily//etc/cron.weekly//etc/cron.monthly/
Dropping a script into the right directory can be simpler than writing a full cron expression for routine maintenance tasks.
Good Practices for Cron Jobs
When writing cron jobs:
- use absolute paths such as
/usr/bin/python3 - redirect output to logs or monitoring
- keep scripts idempotent when possible
- test the script manually before scheduling it
- make sure the script has execute permission
A script that depends on your interactive PATH, aliases, or current directory is likely to break under cron.
systemd Timers as a Modern Alternative
Many modern Linux systems use systemd timers as a more structured alternative to cron. Timers pair with systemd service units and often provide clearer logging, dependency handling, and status inspection.
A timer based workflow might look like this:
systemctl list-timers
sudo systemctl enable --now backup.timer
journalctl -u backup.service
Cron is still widely used and worth learning, but systemd timers are often preferred for services that already live in the systemd ecosystem.
A Practical Troubleshooting Flow
If a cron job did not run, check:
- whether the schedule is correct
- whether the script path is absolute
- whether the command works manually
- whether output was redirected somewhere useful
- whether the cron service is running on that host
A job that silently fails is often missing environment variables, permissions, or full command paths.
What You Should Remember
Cron is a lightweight and powerful scheduler built around time expressions and shell commands. Use crontab -e to edit, crontab -l to inspect, and system cron directories for broader maintenance tasks.
For modern service-based workflows, systemd timers can offer better observability, but cron remains a core Linux skill.
Test Your Understanding
Exercise 1: Reading a cron expression
What does the schedule `*/5 * * * *` mean?
Exercise 2: Editing scheduled tasks
Which command opens the current user's crontab for editing?
Exercise 3: Limited cron environment
Why do many cron jobs use absolute command paths like `/usr/bin/python3`?