Operating System - HP-UX
1753274 Members
4874 Online
108792 Solutions
New Discussion юеВ

Re: Seeking crontab solution

 
SOLVED
Go to solution
ROBIN METZLER
New Member

Seeking crontab solution

I need to schedule two shell scripts to run in the cron scheduler every two weeks on a sunday evening and every two weeks on a wednesday. I know that a can schedule the jobs for the first and fifteenth day of the month, however this is not what I need. The scripts need to run every 14 days to coincide with a payroll cycle. I may be overlooking the obvious solution, but I can't seem to find a workable answer.

Thanks in advance for any assistance.
7 REPLIES 7
Nicolas Dumeige
Esteemed Contributor
Solution

Re: Seeking crontab solution

Why not schedule the two scripts sunday and wednesday - day of the week column - and make the script check for the last run, for instance with a flag file or the last log.

Have you consider using the at command ?
All different, all Unix
Todd McDaniel_1
Honored Contributor

Re: Seeking crontab solution

Sounds like a great candidate for a script to be run at boot as a startup script.

I believe cron would be too convoluted a way to do it, since it could be any Sunday and any Wednesday from month to month.


Just modify your scripts to check a 14 day interval then check the day of the week to be sure it is the correct day.

Or you can hardcode it to check the Julian date.
Unix, the other white meat.
Geoff Wild
Honored Contributor

Re: Seeking crontab solution

Check out Clay's datehammer:

http://www.cmve.net/~merijn/#Contrib

Though the link seems to be down at the moment...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
G. Vrijhoeven
Honored Contributor

Re: Seeking crontab solution

A. Clay Stephenson
Acclaimed Contributor

Re: Seeking crontab solution

The key to doing what you want is to put some smarts inside your cron'ed script to determine if this is the payroll week. Your respective cron jobs should run every Wednesday and every Sunday.

The key to this is caljd.sh; with no arguments it will return the current Julian Day (number of days since 4712 BCE). You divide that by 7 to get the week and then a mod 2 will yield 1 or 0 to get you every other week.

In your cron'ed script or cron'ed wrapper script do this:

payroll_wk()
{
typeset -i10 WK=$((($(caljd.sh) + 1) / 7))
typeset -i10 WK2=$((${WK} % 2))
return ${WK2}
} # payroll_wk

typeset -i10 THIS_WK=$(payroll_wk)
if [[ ${THIS_WK} -eq 0 ]]
then
echo "Do it"
else
echo "Don't; exit"
fi

You may need to change the logic so that if 1 you do it and also note that $(caljd.sh) + 1 is used so that the new week starts on Sun. Becuase of this, your "Sunday" logic will probably be different from your "Wednesday" logic.

Invoke as caljd.sh -u for full usage and examples.

If it ain't broke, I can fix that.
Paula J Frazer-Campbell
Honored Contributor

Re: Seeking crontab solution

Todd has it.

Set in cron an entry to run every day at the correct start time.
In the script check if day of week is Sun or Wed and also check from a flag file if it is 14 days since last run time.


at the end of script if it is run then touch a flag file.


Paula
If you can spell SysAdmin then you is one - anon
ROBIN METZLER
New Member

Re: Seeking crontab solution

Thanks to everyone for your input. This has rejunvinated my creative juices to solve an apparently simple task.

Thanks for your time. I appreciate it.