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
- What is the purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]