Operating System - HP-UX
1753792 Members
7008 Online
108799 Solutions
New Discussion юеВ

scheduling a cron job on the last day of the month

 
SOLVED
Go to solution
Singaram
Advisor

scheduling a cron job on the last day of the month

Is there any way to schedule a job on the last day of every calendar month. Ex on Jan 31st or feb 28th or march 31st or April 30 the etc.

I did solve in round about way.Like to know the best way.

Singaram
16 REPLIES 16
Pete Randall
Outstanding Contributor

Re: scheduling a cron job on the last day of the month

There's no direct way to do this via cron. You'll have to set up a cron job that invokes a script that figures out the last day of the month.


Pete


Pete
A. Clay Stephenson
Acclaimed Contributor

Re: scheduling a cron job on the last day of the month

One method is to literally run the script everyday and let it decide if this is the last day of the month w/o doing the 30 days hath September ...

Note: Cron has a very limited PATH so make sure that whereever you install the attached caljd.sh, the first thing that your cron script does is sets and exports PATH

#!/usr/bin/sh

export PATH=/usr/bin:/usr/local/bin
if [[ $(caljd.sh -M) -ne $(caljd.sh -n 1 -M) ]]
then
echo "Last day of month; your commands go here"
fi

This compares the current date's month with the next days (-n 1). If they differ then it's the last day of the month.

If it ain't broke, I can fix that.
Dave La Mar
Honored Contributor

Re: scheduling a cron job on the last day of the month

Singaram -
As per Pete's suggestion:
TZ=PST8PDT-24 date +%d | read DOM
if [ "$DOM" -eq 1 ]
then
do your thing
fi
TZ=PST8PDT-24 date +%d | read DOM==> says DOM is equal to the day of the month following today.

Best of luck.

Regards,
dl
"I'm not dumb. I just have a command of thoroughly useless information."
john korterman
Honored Contributor

Re: scheduling a cron job on the last day of the month

Hi,
I hereby quote Craig Rants from Favourite Sysadmin Scripts:

There is not much to it, but it always solves the problem of running a script on the last day of the month. Put 28-31 (for days) in your crontab and put this little section in for date determination.

#!/bin/sh

if test `TZ=MET-24 date +%d` = 01
then
exec command
else
exit 1
fi

unqoute.

regards,
John K.
it would be nice if you always got a second chance
John Poff
Honored Contributor
Solution

Re: scheduling a cron job on the last day of the month

Hi,

I always like playing with hacks like this
that use the 'cal' command to get the answer.

Something like this could work:

#!/bin/sh

# lastday.sh

TODAY=$(date +\%d)
LASTDAY=$(cal | tail -2 | head -1 | awk '{print $NF}')

if [[ $TODAY = $LASTDAY ]];
then
echo "Last day of the month."
else
echo "Not the last day of the month."
fi



JP
Singaram
Advisor

Re: scheduling a cron job on the last day of the month

Thanks JP.
I like yours as it is very simple.
But still i dont like the idea of running the script every day. Having found the last day cant we add the cron job for the last day thru a script.

Thanks again
Singaram
Kasper Haitsma
Trusted Contributor

Re: scheduling a cron job on the last day of the month

Singaram,

Like John Korteman suggested, you do not have to run it every month day, only the last couple, starting the 28th:

28-31
so it runs maximum 4 days per month, for a short while..

p.s. would be nive, if you gave points to the solutions you appreciate..
It depends
V. Nyga
Honored Contributor

Re: scheduling a cron job on the last day of the month

Hi Singaram,

you don't need to run it every day, it will do if you run it at the 28th till 31th of every month.

Volkmar
*** Say 'Thanks' with Kudos ***
Singaram
Advisor

Re: scheduling a cron job on the last day of the month

volkumar

I want to run a cron job only on the first of the month, which finds out the last day of the month and should automatically schedule the month end job on the last day.

Now my question is "Is there a way to schedule a cron job automatically from a script?"

Thanks to all

Kasper haitsma

I will assign points as soon as i conclude this discussion.

Singaram