Showing results for 
Search instead for 
Did you mean: 


Go to solution
Super Advisor


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

0 0 1-7 * 6
Chris Wilshaw
Honored Contributor

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


DATE_DAY=`date +%d`

if [ $DATE_DAY -le 7 ]

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?

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. is on

I'm attaching a slightly out of date copy.

Steven E Protter
Owner of ISN Corporation
Michael Schulte zur Sur
Honored Contributor

Re: cron


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

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.,

$./ 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.

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 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. -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 [[ $( -N) -eq 1 && $( -N -p 1) -eq 1 ]]
echo "Do your thing"
echo "Exit; do nothing"

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 -u for full usage.

If it ain't broke, I can fix that.
Michael Schulte zur Sur
Honored Contributor

Re: cron


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

Super Advisor

Re: cron

Chris, good answer. Simple method.

Thanks all.