cron
Schedule recurring jobs on Unix-like systems.
Crontab basics
- Edit your crontab
crontab -e
- List crontab
crontab -l
- Crontab time fields
minute hour day-of-month month day-of-week command
Special strings
@reboot,@yearly,@monthly,@weekly,@daily,@hourly
@daily /usr/local/bin/backup
Examples
- Every 5 minutes
*/5 * * * * /path/script.sh
- At 2:30am daily
30 2 * * * /path/cleanup.sh
- Weekdays at 9:00
0 9 * * 1-5 /path/report.sh
- First day of month at 01:15
15 1 1 * * /path/monthly.sh
Environment & logging
- Set
PATHandSHELLat top of crontab
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- Redirect output to log
0 * * * * /path/job.sh >> /var/log/job.log 2>&1
Common gotchas
- Use absolute paths for commands and files.
- Cron has a minimal environment; source env if needed.
0 * * * * . $HOME/.profile && /path/job.sh
System cron locations
- User crontab:
crontab -e - System-wide:
/etc/crontab,/etc/cron.d/*,/etc/cron.{hourly,daily,weekly,monthly}