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

  1. Edit crontab:
crontab -e
  1. Add a job (every day at 2 AM):
0 2 * * * /path/to/backup.sh
  1. List jobs:
crontab -l
  1. 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.guru to validate expressions.

Common issues

  • Environment variables in cron differ from your shell; set PATH explicitly.
  • Scripts not executable: ensure the file has execute permission and correct shebang.