Bird
0
0

How would you write a crontab entry to run a backup script at 11:59 PM on the last day of every month?

hard📝 Application Q9 of 15
Linux CLI - Cron and Scheduling
How would you write a crontab entry to run a backup script at 11:59 PM on the last day of every month?
A59 23 31 * * /path/backup.sh
B59 23 * * * /path/backup.sh
C59 23 * * 31 /path/backup.sh
D59 23 28-31 * * [ $(date -d tomorrow +\%d) = 01 ] && /path/backup.sh
Step-by-Step Solution
Solution:
  1. Step 1: Understand last day of month challenge

    Crontab cannot directly specify last day; use days 28-31 and test if tomorrow is day 1.
  2. Step 2: Analyze 59 23 28-31 * * [ $(date -d tomorrow +\%d) = 01 ] && /path/backup.sh

    Runs at 23:59 on days 28-31 and uses shell test to check if next day is 1, meaning last day.
  3. Final Answer:

    59 23 28-31 * * [ $(date -d tomorrow +\%d) = 01 ] && /path/backup.sh -> Option D
  4. Quick Check:

    Use day range + shell test for last day detection [OK]
Quick Trick: Use day 28-31 plus shell test for last day [OK]
Common Mistakes:
  • Using day 31 only (fails for months with fewer days)
  • Misusing day of week field
  • Ignoring shell test necessity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes