Cron¶
17 cards — 🟢 3 easy | 🟡 5 medium | 🔴 3 hard
🟢 Easy (3)¶
1. What are the five fields in a cron schedule expression, and what values does each accept?
Show answer
minute (0-59), hour (0-23), day of month (1-31), month (1-12 or JAN-DEC), day of week (0-7 or SUN-SAT, where 0 and 7 both mean Sunday). Example: 30 2 * * * runs at 2:30 AM daily. Use */N for intervals (e.g., */5 for every 5 minutes).Remember: five fields: minute (0-59) hour (0-23) day-of-month (1-31) month (1-12) day-of-week (0-7). Mnemonic: 'Must Have Delicious Meals Daily.'
Example: 0 2 * * 1 = 'at 2:00 AM every Monday.' */15 * * * * = 'every 15 minutes.' 0 0 1 * * = 'midnight on the 1st of every month.'
2. How do you list, edit, and remove a user's crontab?
Show answer
crontab -l to list, crontab -e to edit, crontab -r to remove (dangerous, no confirmation). Use crontab -ri for removal with confirmation. To edit another user's crontab as root: crontab -e -uRemember: crontab -e (edit), crontab -l (list), crontab -r (remove ALL — dangerous!). Use crontab -l > backup.txt before editing.
Gotcha: cron jobs run with a minimal environment. Set PATH explicitly or use full paths to commands. Missing PATH is the #1 cron debugging issue.
3. What cron special strings can you use instead of five-field schedule expressions?
Show answer
@reboot (run once at startup), @hourly (0 * * * *), @daily (0 0 * * *), @weekly (0 0 * * 0), @monthly (0 0 1 * *), @yearly (0 0 1 1 *). These are shorthand aliases for common schedules and improve readability.Remember: five fields: minute (0-59) hour (0-23) day-of-month (1-31) month (1-12) day-of-week (0-7). Mnemonic: 'Must Have Delicious Meals Daily.'
Example: 0 2 * * 1 = 'at 2:00 AM every Monday.' */15 * * * * = 'every 15 minutes.' 0 0 1 * * = 'midnight on the 1st of every month.'
🟡 Medium (5)¶
1. Why do cron jobs fail with "command not found" when they work in a normal shell, and how do you fix it?
Show answer
Cron runs with a minimal environment: PATH=/usr/bin:/bin, SHELL=/bin/sh, and no .bashrc/.profile loaded. Fix by:1) using absolute paths in commands,
2) setting PATH at the top of the crontab,
3) sourcing the user's profile in the command, or
4) using a wrapper script that sets up the environment. This is the number-one cron debugging issue.
2. What is the difference between a user crontab and a system crontab in /etc/cron.d/?
Show answer
User crontabs (crontab -e) have 5 fields + command and run as the editing user. System crontabs (/etc/cron.d/) have 5 fields + a user field + command (6th field specifies which user runs the job). System crontabs can set environment variables at the top and are preferred for config-managed deployments (Ansible, etc.).Remember: comparison questions are best answered with a structured format: name the key dimensions (use case, performance, complexity, cost) and compare each.
3. How do you prevent a cron job from running multiple overlapping instances?
Show answer
Use flock: */5* *
* * root flock -n /var/lock/job.lock /usr/local/bin/job.sh. The -n flag makes it non-blocking (exit immediately if lock exists). The -w 60 flag waits up to 60 seconds. This prevents the most common cron disaster: a job that runs longer than its interval causing overlapping instances and corrupted data.
4. What advantages do systemd timers have over traditional cron?
Show answer
Structured logging via journald (vs mail or redirect), dependency management (After=, Requires=), resource limits (CPUQuota=, MemoryMax=), missed-run recovery (Persistent=true), random delay to spread load (RandomizedDelaySec=), built-in overlap prevention (oneshot type), calendar expression validation (systemd-analyze calendar), and unified status checking (systemctl status).Remember: systemd timers are the modern alternative to cron. Benefits: dependency management, better logging (journalctl), calendar events, and persistent timers that run after missed schedules.
5. How should you handle output and logging for cron jobs?
Show answer
Redirect stdout and stderr to a log file: command >> /var/log/job.log 2>&1. Set MAILTO=ops@example.com to email any unredirected output. Suppressing all output (> /dev/null 2>&1) should be used sparingly as it hides errors. For systemd timers, logs go to journald automatically (journalctl -u service.service).Remember: cron sends output via email by default. Redirect: */5 * * * * /script.sh >> /var/log/myscript.log 2>&1. Set MAILTO='' to disable email.
🔴 Hard (3)¶
1. How do you create a systemd timer that runs a backup daily at 2:30 AM with missed-run recovery?
Show answer
Create two files: a timer (/etc/systemd/system/backup.timer) with [Timer] OnCalendar=*-*-* 02:30:00, Persistent=true (runs on boot if missed), RandomizedDelaySec=300. And a service (/etc/systemd/system/backup.service) with Type=oneshot, ExecStart=/usr/local/bin/backup.sh. Enable with systemctl enable --now backup.timer. Validate with systemd-analyze calendar "*-*-* 02:30:00".Remember: systemd timers are the modern alternative to cron. Benefits: dependency management, better logging (journalctl), calendar events, and persistent timers that run after missed schedules.
2. What happens when you specify both day-of-month and day-of-week in a cron expression?
Show answer
In standard cron, specifying both creates an OR condition, not AND. For example, "0 0 15* 1" runs at midnight on the 15th of every month OR every Monday, not only on Mondays that fall on the 15th. This is a common gotcha that leads to jobs running more often than expected. Systemd timers use AND logic instead.
3. How does a Kubernetes CronJob handle missed runs and overlapping instances?