Need help in running script on last day of month

Hello Experts/Guru,

I need a help in running the script on every month last day....

PS: due to some constrain I can't schedule in crontab

Requirement:

On Jan 31st i want to run some script, similarly on Feb 28th, March 31st, April 30th......till Dec 31st.

Please help me by providing me a script...

Thanks a lot for your help in advance

check this thread .

Hello Amit,

Can you please help me clearing one doubt....

If i run the below script...

#!/bin/bash

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

# See if tomorrow's day is less than today's
if [ $TOMORROW -lt $TODAY ]; then
        exit 0
fi

exit 1

it will run for every day which less than tomorrow, like it will on 29 coz 29 is less than 30, it will run on 30 coz 30 is less than 31st....what happen if date is 28th Feb...!!!

and my requirement is it should run on last day of every month....I can't do scheduling in crontab, due to some constrain......please help me in achieving it through script.....

Thanks in advance

Try this...

#!/bin/bash
day=$(date +%d)
month=$(date +%m)
year=$(date +%Y)
run_or_not=$( awk -v day=$day -v month=$month -v year=$year 'BEGIN {
            if(month%2 && day == 30){ print "run" }else {
            if(month == 2){ leap=(0 == year % 4 && 0 != year % 100 || 0 == year % 400)
            if(leap && day == 29 ){ print "run" } else if(!leap && day == 28){ print "run"}
            } else if(day == 31){ print "run" } } }'  )
if [ "$run_or_not" == "run" ]
then
  echo "Execute your script!"
fi

--ahamed

1 Like