Run a CRON job on the last day of the month?

Better Stack Team
Updated on May 26, 2023

To run a cron job on the last day of the month, you can use the following format in your crontab file:

 
0 0 28-31 * * [ $(date -d "+1 day" +%d) = "01" ] && /path/to/command

This cron job will run at midnight (0 hours and 0 minutes) on the 28th, 29th, 30th, or 31st of the month. The command to be executed will only run if the next day is the 1st of the next month, which means it's the last day of the current month. Replace /path/to/command with the command you want to run.

Here's a breakdown of the format:

  • The first 0 represents the minute (0-59)
  • The second 0 represents the hour (0-23)
  • The third field, 28-31, means the job will run on the 28th, 29th, 30th, or 31st day of the month.
  • The ***** in the fourth and fifth positions represent the month and day of the week (respectively). Using ***** means the job will run every month and every day of the week.
  • The sixth field is a command that checks if the next day is the 1st of the next month using the date command. If it is, then the command specified after the && will be executed.

So the cron job in the above format will run on the last day of the month.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack