1834142 Members
1860 Online
110064 Solutions
New Discussion

CRON Help Needed.

 
SOLVED
Go to solution

CRON Help Needed.

I want to set my cron to run a script on the first Friday of the month at 7:01 and only on the first Friday of the month. I have tried to set it using the following crontab line
01 7 1-7 * 5 /scriptname
However, instead of only running the script on a Friday that is also the 1st, 2nd, 3rd, 4th, 5th, 6th, or 7th of the month, it will run it every Friday and also every day of the month between the 1st and 7th.
5 REPLIES 5
Pete Randall
Outstanding Contributor

Re: CRON Help Needed.

William,

The easiest way to accomplish this would be to put the logic inside your script to determine if it's Friday or not. Cron has no mechanism for this.


Pete

Pete
Dave Johnson_1
Super Advisor

Re: CRON Help Needed.

Another option is to run it every friday and test to see if todays date is <= 7.

-Dave
Mark Grant
Honored Contributor
Solution

Re: CRON Help Needed.

Yep, cron can't do this

You should get cron to run the thing every Friday but add the following to the top of the script

[ `date +%d` -gt 6 ] && exit
Never preceed any demonstration with anything more predictive than "watch this"
A. Clay Stephenson
Acclaimed Contributor

Re: CRON Help Needed.

Your fundamental problem is that cron's logic is OR'ing your request and you really need an AND. Cron won't do this on its own but my solution for all things date related is caljd.sh.

In your case, the fix is to have cron run your script every Friday and then add a test within the script itself to determine if this is the 1st (or 2nd, 3rd) ...

if [[ $(caljd.sh -N) -eq 1 ]]
then
echo "First Friday; Do your thing"
else
echo "It ain't, exit"
fi

If it ain't broke, I can fix that.

Re: CRON Help Needed.

Thanks for the help. I kind of figured that the wasn't a way to set it up in the cron. I'll just schedule it for every Friday and have my script exit if the date isn't with in the 1st through 7th range. Thanks again everyone.