1830401 Members
2285 Online
110002 Solutions
New Discussion

Doubt in crontab

 
Amadeus_1
Regular Advisor

Doubt in crontab

Hi,

i want to schedule the cron job in crontab on 2nd sunday of every month.

Is it possible?if so how to do it..

help apperciated .
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor

Re: Doubt in crontab

Not directly with cron, however you can run a script every Sunday and then let the script decide if it is the 2nd Sunday and take action or simply exit if not. The attached script, caljd.sh, will make this easy. I'll assume that you install it in /usr/local/bin. You script invoked by cron each Sunday should look very much like this:

------------------------------------------
#!/usr/bin/sh

typeset -i STAT=0

export PATH=${PATH}:/usr/local/bin

if [[ $(caljd.sh -N) -eq 2 ]]
then
echo "2nd Sunday; do your thing."
STAT=${?}
fi
exit ${STAT}
--------------------------------------------
Here's the caljd.sh attachment; invoke it as caljd.sh -u for full usage and many examples.

If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: Question on crontab

>Clay: you can run a script every Sunday and then let the script decide if it is the 2nd Sunday

You might want to reverse that and run it for every second week and then have the script test for Sunday. (Even though it fires off 7 days a month instead of 4 or 5, the scripting is easier.)

# The test for Sun is in the script.
00 05 08-14 * * gen_monthly_status

WD=$(date +"%a")
if [ $WD != "Sun" ]; then
    exit
fi

If you are worried about locales, you may want to use "%u" and check for 7.

SANTOSH S. MHASKAR
Trusted Contributor

Re: Doubt in crontab

Hi,

By using cron it is possible to schedule jobs
on all Sundays/Weekdays, for schjeduling on Sundays

If u want to schedule a job on every 2nd Sunday,
u schedule the job to run on all sundays and
check in the job itself if it is 2nd sunday or not.

eg.

ur cronfile may look like this.
--------------------------
0 0 20 * * 0 /
--------------------------

Add code to check occurance of Sunday in job.sh

job.sh
----------
#! /usr/bin/ksh

Day=`date +'%d'`
Week=`echo "scale=0;($Day + 6) / 7"|bc -l`

if [ $Week -eq 2 ]
then
fi