Interview › Scripting (Bash, Groovy)
How do you schedule a script with cron, and what do the five cron fields mean? [Advanced]
Answer
I schedule a script with cron by adding an entry to a user's crontab or /etc/cron.d. The five fields are minute, hour, day of month, month, and day of week, followed by the command.
Technical explanation
Cron has a limited environment, so use absolute paths and set required environment variables explicitly.
Redirect output to logs or monitoring; otherwise cron may mail output depending on system configuration.
Use flock or another lock if overlapping runs would be unsafe.
Hands-on example
# Edit current user's crontab
crontab -e
# Run every 5 minutes
*/5 * * * * /usr/local/bin/health-check.sh >> /var/log/health-check.log 2>&1
# Run daily at 02:30
30 2 * * * /usr/local/bin/backup.shPreparing for an interview?
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- How would you write a health-check script that polls a URL until it returns 200? [Advanced]
- What is the difference between a cron job and a systemd timer? [Advanced]
- What is idempotency, and how do you make a script safe to run repeatedly? [Advanced]
- How do you securely handle secrets in a shell script (avoid hardcoding, use env or a vault)? [Advanced]
- What is the difference between sed -i and sed without -i? [Advanced]
- How do you define and call a function (or step) in a Jenkins Groovy pipeline? [Advanced]
- How do you use awk to sum or average a column? [Advanced]
- How do you handle errors and retries in a Groovy pipeline (try/catch, retry, timeout)? [Advanced]