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.sh

Preparing 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

← All Scripting (Bash, Groovy) questions