Operating System - HP-UX
1832609 Members
2456 Online
110043 Solutions
New Discussion

Re: Cron Question/Suggestion.

 
SOLVED
Go to solution
brian_31
Super Advisor

Cron Question/Suggestion.

Team:

I had a question and as well as an answer to the cron scheduling below. Please tell me if i am correct.

1. How I can run after each 14 Days.
2. How I can make sure that the job is running on the first working day of the month.
Taking only Sat-Sun into account for Holidays.

Answer...

For both requirements, some logic has to be built into the program that is to be run thro' cron. The cron tab entry may be set as:
1. <>
2. 0 0 1-7 * * <>

The <> logic should inturn check the day.

Is this correct? Please let me know?

Thanks

12 REPLIES 12
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Cron Question/Suggestion.

Hi Brian,

Yes. For both you will need to build logic into your script itself.

1. Have a script schedule it again after 14 days using 'at' command. "at" command allows you to specify offset in 'weeks'.

2. You can scedule your job to run for the first three days only looking for the following conditions.

Date Don't run Run if
---- --------- -------
1 Sat,Sun Mon - Fri
2 Tue - Sun Mon
3 Tue - Sun Mon

You have to run it on 3rd also because 2nd could be a sunday.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
A. Clay Stephenson
Acclaimed Contributor

Re: Cron Question/Suggestion.

As you have already surmised, cron on its own ain't that smart. We're gonna have to help him some inside your script. The key to what you are trying to do lies in a script that will convert calendar dates (or today's date if no date is supplied) into Julian Days (days which count sequentially from 4713BCE).

For 1)
Make a crontab entry that looks like this:
15 18 * * 1 /usr/local/bin/myscript.sh

to run at 18:15 every Monday, for example. That runs every 7 days so now your script needs some logic to run every other week.

#!/usr/bin/sh

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

typeset -i WEEKNO=$(( ($(cajld.sh) + 1) / 7))
typeset -i WK_0_or_1=$((${WEEKNO} % 2))
if [[ ${WK_O_or_1} -eq 0 ]]
then
echo "Do your thing"
else
exit 0 # don't do nothing
fi

The ($(caljd.sh) + 1) makes each week start on a Sunday.

Now the next one is a little more tricky but we can even expand on your baby-food definition of a workday to exclude real honest-to_God holidays as well as weekends.

Create/update your /etc/acct/holidays file to reflect reality. (You should already have a default and very old version on your box). Caljd.sh actually looks for /etc/acct/holidays_2004 BEFORE it looks for the default /etc/acct/holidays so that this stuff even will work across year boundaries and observe holidays. You should really create a soft-link between holidays_YYYY and holidays for the current year.

Now create a cron entry to run every Monday through Friday; this is overkill but I have no idea how many holidays you have and this won't hurt.

15 9 * * 1-5 /usr/local/bin/myscript2.sh

#!/usr/bin/sh

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

typeset -i TODAYS_MONTH=$(caljd.sh -M)
typeset -i PREV_MONTH_MAYBE=$(caljd.sh -p 1 -x 0 -x 6 -h -M)
if [[ ${TODAYS_MONTH} -ne ${PREV_MONTH_MAYBE} ]]
then
echo "First working day of the month; do your thing"
else
exit 0
fi

The -p 1 says previos day; -x 0 skip Sundays; -x 6 ditto Saturdays; -h skip holidays (as defined in /etc/acct/holidays_YYYY). -M output just the month. Basically if today's month ain't equal to yesterday's month (skipping in the same direction weekends and holidays) then it's the first working day of the month.

I'm assuming you install the attached caljd.sh, in /usr/local/bin. Invoke as caljd.sh -u for full usage and examples.
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: Cron Question/Suggestion.

Well.. the the format of the table got messed up. Let me try this way.

1. If the date ( date +%d) is 1, then run only if the day (date +%a) is neither Saturdy nor Sunday.
2. If the date is 2, then run it only if it is monday.
3. If the date is 3, then run it only if it is monday.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
A. Clay Stephenson
Acclaimed Contributor

Re: Cron Question/Suggestion.

By the way, I would avoid at based solutions that create new atjobs each time your script is run because if for some reason your script does not run, the next at entry is not created so the chain is broken. It's much more robust to use a cron entry.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Cron Question/Suggestion.

For your case 2, there is one more possibility that we need to exclude; today might be in the holidays file so we want to exist immediately. Let's modify the 2nd script to handle that case as well.

#!/usr/bin/sh

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

typeset -i TODAY=$(caljd.sh)
itypeset -i TODAY_HOLIDAY_MAYBE=$(caljd.sh -h) # skip is positive if neither -p days or -n days is specified

if [[ ${TODAY} -ne ${TODAY_HOLIDAY_MAYBE} ]]
then
exit 0 # today's a holiday
fi

typeset -i TODAYS_MONTH=$(caljd.sh -M)
typeset -i PREV_MONTH_MAYBE=$(caljd.sh -p 1 -x 0 -x 6 -h -M)
if [[ ${TODAYS_MONTH} -ne ${PREV_MONTH_MAYBE} ]]
then
echo "First working day of the month; do your thing"
else
exit 0
fi

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

Re: Cron Question/Suggestion.

Clay & Sri:

Great Answers.

Just wanted to know if my answers would work or anything wrong with that?

Thanks

Brian
Sridhar Bhaskarla
Honored Contributor

Re: Cron Question/Suggestion.

Brian,

I imagine you would keep some kind of error checking in your script for failures? Because you are going to add some logic anyway, it doesn't take long adding couple of lines that will either page you or send you a mail so you can fix things easily in which case 'at' doesn't hurt you. Even you can add a 'trap' inside that can automatically 'at' the job in case of any failures. You can also put some logic with week numbers given by 'date' for case 2. But you already got a cooked script/program so it's just a food for thought.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
brian_31
Super Advisor

Re: Cron Question/Suggestion.

Hi Sri:

Thanks. i have never used at. If i have to use "at" then could you give me the commands for my question?

Thanks Again,

Brian
Sridhar Bhaskarla
Honored Contributor

Re: Cron Question/Suggestion.

Hi (Again),

I kept clicking submit and finally I could post the last message. Regarding your answers -

1. I don't think "" is a valid cron entry. ;-).
2. Taking only Sat-Sun as holidays, you don't need to run the script everyday from 1st to 7th.

I believe you already got a working solution in hand so you don't have to work hard unless you want to devise one yourself.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: Cron Question/Suggestion.

Hi,

'man at' gives you what you need. To schedule a job after 14 days or 2 weeks run

at now + 2 weeks < your_job

To list your at jobs

at -l

An example to use 'trap' in case of any failures.

schedule()
{
at now + 2 weeks < your_job > /tmp/log$$ 2>&1
mailx -s "at status" your_id@yourmail.com < /tmp/log$$
rm -f /tmp/log$$
}

trap schedule 0 1 2 3 4

Put the above before the contents of your script

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: Cron Question/Suggestion.

OK.. You gave me more than enough points already. So, please award '0' for this one and the last one and any unassigned so far... Thanks.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Singaram
Advisor

Re: Cron Question/Suggestion.