Chapter 5: Bash Scripting for Automation
5.6 Cron basics — schedule the script
In short: cron runs commands on a schedule on Linux.
cron runs commands on a schedule on Linux.
crontab -e
Example line (runs every five minutes — adjust when you understand the fields):
*/5 * * * * /home/ec2-user/bin/health_restart.sh >> /home/ec2-user/badge-api-logs/health.cron.log 2>&1
| Field (left → right) | Meaning |
|---|---|
| minute | 0–59 |
| hour | 0–23 |
| day of month | 1–31 |
| month | 1–12 |
| day of week | 0–7 (Sunday both ends on many systems) |
PATH trap. Interactive shells load your fancy PATH. Cron often has a minimal PATH. Prefer absolute paths to the script and to binaries (/usr/bin/curl), or set PATH= at the top of the script.
Ravindra Bagale's Tip
Script tumchya terminal madhe perfect, cron madhe fail — khup common. Absolute paths, env vars cron madhe set, aani log file redirect (>> … 2>&1) lakshat theva. Bilkul visru naka!
Lab VM reminder
Use the practice EC2 from the AWS course. Stop idle instances when finished. This chapter does not re-teach launch or Security Groups.