Operating System - HP-UX
1819935 Members
3369 Online
109607 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
V. Nyga
Honored Contributor

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

Hi Singaram,

I'm not good in shell scripting, it should be possible, but you have to rewrite your crontab file '/var/spool/cron/crontabs/root' every month:
At the first day you have to check (with cron) which month you have. Then set the last day (for January, March, May ... 31th for example), then read you crontab file, delete (for example) the last line and add a new one with the current 'last day'.
Then write the new crontab file.

Good luck
Volkmar
*** Say 'Thanks' with Kudos ***
Kasper Haitsma
Trusted Contributor

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

you could also add 12 cron lines, one for each month..
It depends
Elmar P. Kolkman
Honored Contributor

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

In that case, try looking at 'at'. With that you can do what you want: schedule a script from a script.
Or use crontab -l | | crontab

The commands to change the script line would become something like this:

awk '/<scriptname>/ { $5='$LASTDAY'}
{print}'

As long as LASTDAY contains the last day of the month like in JP's script, it work.
Of course there are cleaner ways to do it, but is should work.
Every problem has at least one solution. Only some solutions are harder to find.
Pete Randall
Outstanding Contributor

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

Singaram,

To do it your way, run the cron job on the first of the month to calculate the end of the month day (let's call it $EOM, "Oct 31, for example). Then invoke an at job to run your end of month job thusly:

at -f /full/path/of/your/job $EOM


Pete



Pete
Singaram
Advisor

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

JP

I have to find the last day of the month avoiding saturday and sunday.

Any short cuts on awk?

Thanks
Sing

A. Clay Stephenson
Acclaimed Contributor

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

You are still doing this the hard way. THe overhead of running a cronjon once per day is trivial. Setup your cron to run Mondays thru Friday (1-5) and then make one small change to my first solution.

if [[ $(caljd.sh -M) -ne $(caljd.sh -n 1 -x 6 -x 0 -M) ]]
then
echo "Do your thing"
fi

That says compare today's month with tomorrow's month (-n 1) unless that day falls on a Saturday -x 6 or a Sunday -x 0. This has the effect of running either on the last day of the month or on the last Friday.
Want to get more complicated? Add -h and it will skip holidays as well if your /etc/acct/holidays is setup. Invoke as caljd.sh -u for full usage.


If it ain't broke, I can fix that.
John Poff
Honored Contributor

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

Hi Sing,

My little hack with 'cal' will find the last day of the month, but if you need it to be a weekday the hack gets very tricky.

Probably the easiest thing for you to do is to use Clay's script. His script handles all kinds of date calculations and is very handy to have in your kit.

JP