- cron = time-based job scheduler in Unix/Linux.
- Runs background process
crondto execute commands or scripts at scheduled times. - Jobs are defined in crontab (cron table).
Each cron job has 5 fields + command:
* * * * * command-to-execute
| | | | |
| | | | βββ Day of week (0-6, Sun=0)
| | | βββββ Month (1-12)
| | βββββββ Day of month (1-31)
| βββββββββ Hour (0-23)
βββββββββββ Minute (0-59)
* * * * * echo "Hello" >> /tmp/hello.log30 2 * * * /home/user/backup.sh0 17 * * 1 /home/user/report.sh*/10 * * * * /home/user/script.sh@reboot /home/user/startup.shInstead of 5 fields, you can use shortcuts:
| Keyword | Meaning |
|---|---|
@reboot |
Run at system startup |
@yearly |
Once a year (Jan 1, 00:00) |
@monthly |
Once a month |
@weekly |
Once a week |
@daily |
Once a day (midnight) |
@hourly |
Once every hour |
crontab -ecrontab -lcrontab -r- Write your script, e.g.
/home/user/backup.sh
#!/bin/bash
tar -czf /home/user/backups/backup_$(date +%F).tar.gz /home/user/data- Make it executable:
chmod +x /home/user/backup.sh- Add to cron (e.g., daily at 1 AM):
0 1 * * * /home/user/backup.sh- Redirect output/errors to log:
0 3 * * * /home/user/myscript.sh >> /var/log/myscript.log 2>&1- Check logs:
journalctl -u cron
# or
grep CRON /var/log/syslog # Debian/Ubuntu
grep CROND /var/log/cron # RHEL/CentOSIf you want a command once, use at:
echo "/home/user/script.sh" | at 10:30π Runs once at 10:30 AM today.
β Summary:
- Use cron for repetitive scheduling.
- Use @keywords for simple scheduling.
- Use at for one-time jobs.
- Always log output for debugging.