How do I schedule a cron job on Linux?
The five time fields
Cron reads one schedule per line. The five fields are minute, hour, day of month, month and day of week, in that order. A star means every value, so 0 2 * * * runs at minute 0 of hour 2 every day.
Use full paths in your commands, such as /usr/bin/python3 or /home/user/backup.sh, because cron runs with a minimal environment. Redirect output to a log file so you can see errors, for example >> /var/log/backup.log 2>&1.
- */15 * * * * runs every 15 minutes
- 0 3 * * 0 runs Sundays at 3:00 a.m.
- @reboot runs once at startup on many systems
- crontab -l lists your current jobs
Check that it works
Run the script manually first, using the exact same path and user. Then check the log file after the scheduled time passes. If nothing happens, confirm the cron service is running with systemctl status cron, or crond on some distributions.
Cron jobs run as the user whose crontab you edit, so use sudo crontab -e only when the job truly needs root. The schedule uses the server local time zone by default, so check it with timedatectl.
Common mistakes
- Using relative paths that work in your terminal but fail under cron.
- Forgetting to redirect output, so failures happen silently.
- Leaving old jobs in the crontab after a script moves, then wondering why it still runs.
