cron runs commands on a schedule on Linux servers. Bash scripts are common cron targets—keep them idempotent and log output.
Crontab format
# min hour dom month dow command
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
Fields: minute, hour, day-of-month, month, day-of-week. Use crontab -e for your user table.
Script hygiene for cron
#!/usr/bin/env bash
export PATH="/usr/local/bin:/usr/bin:/bin"
cd /opt/app || exit 1
./run-job.shCron environments are minimal—set PATH and cd explicitly.
Alternatives
systemd timers, GitHub Actions schedules, and cloud schedulers replace cron in many stacks—Bash glue scripts stay the same pattern: exit codes and logs.
Important interview questions and answers
- Q: Why set PATH in cron scripts?
A: Cron provides a sparse environment—commands may not be found otherwise. - Q: Redirect cron output?
A: Capture stdout/stderr to log files for debugging silent failures.
Self-check
- How many time fields does a cron line have?
- Why cd to the app directory first?
Tip: Log cron output somewhere visible—silent failures are hard to debug.
Interview prep
- Cron fields?
minute hour day month weekday command.
- Sparse cron PATH?
Set PATH explicitly in cron scripts.