Re: cron (239 Views)
Reply
Super Advisor
jerry1
Posts: 604
Registered: ‎10-26-2000
Message 1 of 9 (239 Views)
Accepted Solution

cron

Does anyone know how to make a cron job run
only on the first weekend of the month with
saturday and sunday being in that month, not
split. And starting on saturday. Don't think
it can be done with cron.

The following crontab entry runs on the 1st
thru the 7th and "also" on saturday not "if"
it is saturday within the 1st - 7th day of the
month.

0 0 1-7 * 6
Please use plain text.
Honored Contributor
Chris Wilshaw
Posts: 696
Registered: ‎04-09-2002
Message 2 of 9 (239 Views)

Re: cron

Sadly, cron's not sophisticated enough to handle this without help.

You need to set the job to run on the saturday, then have logic within the script to test if the day is in the first seven days of the month

eg:

DATE_DAY=`date +%d`

if [ $DATE_DAY -le 7 ]
then
......
......
fi

This checks if the current day is less than or equal to 7, and runs the code in the if statement.
Please use plain text.
Respected Contributor
Hazem Mahmoud_3
Posts: 256
Registered: ‎11-22-2003
Message 3 of 9 (239 Views)

Re: cron

The only option I can think of is to have it run every Saturday and Sunday and then put some logic in the job to determine if it is the first weekend of the month or not. If it is, proceed with the rest of the job (which executes the command), if not then exit out of the script.
Do you see what I mean?

-Hazem
Please use plain text.
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 4 of 9 (239 Views)

Re: cron

The job needs to run every day and determine what day it is.

A. Clay Stephenson has some great utilities that help you figure that out. caljd.sh is on http://www.hpux.ws/merijn

I'm attaching a slightly out of date copy.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Please use plain text.
Honored Contributor
Michael Schulte zur Sur
Posts: 4,040
Registered: ‎06-18-2003
Message 5 of 9 (239 Views)

Re: cron

Jerry,

unfortunately the crontab positions are not all connected with and but or.
Use as Chris suggests.

Michael
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 6 of 9 (239 Views)

Re: cron

Hi Jerry,

It cannot be done by default with cron. However, you can add that logic into your script to see if this day is first saturday and exit if not. Attached is a script that is to be prefixed to your orignal script. This checks if today is the day you specified via arguments. For ex.,

$./croncheck.sh 1 2
Usage ./ck.cron: Week Weekday
Example: ./ck.cron 1 Fri - for first friday
Your schedule is error
$./croncheck 1 sat
Your schedule is notok
$./croncheck 1 fri
Your schedule is ok

Edit the script and put exit instead of echoing MYSCHEDULE>

Run the whole script (this script + your script) on every saturday with the arguments 'croncheck 1 sat'. It will exit on all other saturdays except first saturday.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 7 of 9 (239 Views)

Re: cron

The version of caljd.sh is not recent enough to solve this problem easily. The problem with trying to use cron on it's own it that the monthday and weekday fields do an OR and you intended an AND. As suggested, you need to run the script every Saturday and then use a mechanism to determine if it is ther correct Saturday.

Actually, you probably want this to run at 00:00 SUNDAY rather than Saturday if I understand your question.

I would change your crontab entry to:
0 0 * * 0

and then add this to your cronjob script. Make sure that PATH is set and exported because cron's environment is intentionally sparse.

I think I understand your question. You require that BOTH Saturday and Sunday be the first instance in this month. If Sunday is the 1st Sunday of the current month but Saturday is the last Saturday of the prior month, you do not want to trigger any action.

Caljd.sh -N's output indicates the occurrance of a current weekday withing the month (1 = 1st; 2 = 2nd; ...)



This should be added in your cronjob script:

if [[ $(caljd.sh -N) -eq 1 && $(caljd.sh -N -p 1) -eq 1 ]]
then
echo "Do your thing"
else
echo "Exit; do nothing"
fi

We are checking the occurance of today's weekday (-N) and also the occurance of the previous day (-N -p 1). Only if both equal 1 do we proceed.

Invoke as caljd.sh -u for full usage.

If it ain't broke, I can fix that.
Please use plain text.
Honored Contributor
Michael Schulte zur Sur
Posts: 4,040
Registered: ‎06-18-2003
Message 8 of 9 (239 Views)

Re: cron

ACS,

why would you want to run the script on sunday? Remember the Vulcan High Councel does not believe in time travel! ;-)

Michael
Please use plain text.
Super Advisor
jerry1
Posts: 604
Registered: ‎10-26-2000
Message 9 of 9 (239 Views)

Re: cron

Chris, good answer. Simple method.

Thanks all.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation