Cron Schedules

Last Day of Month (Workaround)

Run on the last day of each month - requires script validation.

Pattern

0 0 28-31 * *

Explanation

Runs on days 28-31, with script checking if it's actually the last day. Standard cron has no direct "last day" syntax.

Examples

Cron Expression
Output
0 0 28-31 * *
Note
Output
Requires date validation in script

Code Examples

Bash Script
# Crontab entry
0 0 28-31 * * /path/to/last-day-check.sh

# Script: last-day-check.sh
#!/bin/bash
# Check if tomorrow is a new month
tomorrow=$(date -d tomorrow +%d)
if [ "$tomorrow" = "01" ]; then
    echo "This is the last day of the month!"
    # Run your end-of-month task
    /path/to/month-end-task.sh
else
    echo "Not the last day, skipping..."
fi
Node.js
// Node-cron with last day validation
const cron = require('node-cron');

cron.schedule('0 0 28-31 * *', () => {
  const today = new Date();
  const tomorrow = new Date(today);
  tomorrow.setDate(today.getDate() + 1);

  // Check if tomorrow is day 1 (new month)
  if (tomorrow.getDate() === 1) {
    console.log('Last day of month - running task');
    runMonthEndTask();
  } else {
    console.log('Not last day, skipping');
  }
});
Python
# Python with calendar module
import calendar
from datetime import date, timedelta

def is_last_day_of_month():
    today = date.today()
    last_day = calendar.monthrange(today.year, today.month)[1]
    return today.day == last_day

# In your cron script
if is_last_day_of_month():
    print("Running end of month task")
    run_month_end_task()

Try it Now

💡 Tips

  • Always validate date in script logic
  • Consider systemd timers for better calendar support
  • Test with February (28/29 days)
  • Log all runs and skips for debugging
  • Alternative: check tomorrow's date is 1st
  • Some cron systems support L syntax (check docs)

⚠️ Common Pitfalls

  • Standard cron has no "last day" expression
  • Will trigger 2-4 times per month without validation
  • Leap years affect February
  • Requires additional scripting logic
  • Consider using external schedulers with better date logic