1748171 Members
4434 Online
108758 Solutions
New Discussion юеВ

Re: cron

 
SOLVED
Go to solution
jerry1
Super Advisor

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
8 REPLIES 8
Chris Wilshaw
Honored Contributor
Solution

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.
Hazem Mahmoud_3
Respected Contributor

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
Steven E. Protter
Exalted Contributor

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
Michael Schulte zur Sur
Honored Contributor

Re: cron

Jerry,

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

Michael
Sridhar Bhaskarla
Honored Contributor

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
A. Clay Stephenson
Acclaimed Contributor

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.
Michael Schulte zur Sur
Honored Contributor

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
jerry1
Super Advisor

Re: cron

Chris, good answer. Simple method.

Thanks all.