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:
#!/bin/bash TODAY=`/bin/date +%d` TOMORROW=`/bin/date +%d -d "1 day"` if [ $TOMORROW -lt $TODAY ]; then exit 0 fi 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 * * * isLastDayOfMonth.sh && yourJob.sh