Determine last day of month using Bash script

Oftentimes software systems need to carry out tasks on a regular basis at a given time, such as archiving log files or sending out newsletters. Normally cron is able to handle all kinds of time-based setups (daily, monthly, etc.) but what if you want to execute tasks on the last day of each month?

A simple bash script does the trick:


TODAY=`/bin/date +%d`
TOMORROW=`/bin/date +%d -d "1 day"`

if [ $TOMORROW -lt $TODAY ]; then
   exit 0

exit 1

So, as you can see we need to simply check if the next day is “less” than today. All you need to do is to call this script at the required time each day and it will check if it’s the last day of the current month, like so:

59 23 * * * &&

Thats’s it.

2 thoughts on “Determine last day of month using Bash script”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top