Quiz: Cron & Job Scheduling¶
4 questions
L0 (1 questions)¶
1. What are the five fields in a cron schedule expression and what does '/5 * * * ' mean?
Show answer
The five fields are: minute (0-59), hour (0-23), day of month (1-31), month (1-12), day of week (0-7). '*/5 * * * *' means every 5 minutes — the step value */5 in the minute field triggers at 0, 5, 10, 15, etc.L1 (1 questions)¶
1. Why does a script work when run manually but fail in cron, and how do you fix it?
Show answer
Cron runs with a minimal environment: PATH is only /usr/bin:/bin, SHELL is /bin/sh, and no .bashrc or .profile is loaded. Fix by:1. using absolute paths for all commands,
2. setting PATH at the top of the crontab,
3. sourcing the profile in the command line, or
4. using a wrapper script that sets up the environment.
L2 (1 questions)¶
1. How do you prevent a cron job from overlapping with a previous run that has not finished?
Show answer
Use flock: '*/5 * * * * flock -n /var/lock/myjob.lock /usr/local/bin/myjob.sh'. The -n flag makes flock non-blocking — if the lock exists from a previous run, it exits immediately. For systemd timers, overlap prevention is built in with Type=oneshot. For Kubernetes CronJobs, set concurrencyPolicy: Forbid.L3 (1 questions)¶
1. Compare cron, systemd timers, and Kubernetes CronJobs. When would you choose each?