How to schedule tasks with cron in Linux
· Category: Linux
Short answer
cron is a time-based job scheduler. Edit your crontab with crontab -e to add recurring tasks.
Steps
- Edit crontab:
crontab -e
- Add a job (every day at 2 AM):
0 2 * * * /path/to/backup.sh
- List jobs:
crontab -l
- Remove all jobs:
crontab -r
Tips
- Crontab fields: minute, hour, day of month, month, day of week.
- Redirect output to avoid mail spam:
>> /var/log/job.log 2>&1. - Use
https://crontab.guruto validate expressions.
Common issues
- Environment variables in cron differ from your shell; set
PATHexplicitly. - Scripts not executable: ensure the file has execute permission and correct shebang.