How to schedule tasks with cron

Overview

Cron runs commands on a schedule even when no terminal is open. A user crontab runs as that user and should normally not include sudo.

Edit and inspect

crontab -e
crontab -l
# minute hour day-of-month month day-of-week command
30 7 * * 1-5 /home/alice/bin/report.sh >> /home/alice/report.log 2>&1
0 2 * * 0 /home/alice/bin/backup.sh

Schedule fields and habits

  • Five fields represent minute, hour, day of month, month, and day of week
  • An asterisk means every allowed value
  • Use absolute paths for scripts, commands, and output files
  • Redirect output and errors to a log for troubleshooting
  • crontab -r removes the crontab, so use it with extreme care

Safety and testing

Run the exact command manually first, make scripts executable, and remember cron has a limited environment and a different working directory.

Avoid scheduled destructive commands until logging and backups are proven. Do not embed passwords or depend on interactive sudo prompts.