1830505 Members
2421 Online
110006 Solutions
New Discussion

Cronjob every 10 days?

 
SOLVED
Go to solution
Robert Fisher_1
Frequent Advisor

Cronjob every 10 days?

Hello Gang,

I need to transmit some data files every 10 days at midnite. At the present, I have a crontab entry that runs my transfer script every day and then tests the date '+%j'. If that modulo 10 value is correct, the script executes otherwise it exits without doing any transfers. I am concerned that this may not work when I jump to a new year. Am I on the right track or can I do this with cron alone? Any help would be appreciated.

TIA, Bob
13 REPLIES 13
Tom Geudens
Honored Contributor

Re: Cronjob every 10 days?

Hi,
You are - I think - on the right track. I would - if I were you - use the caldate.sh script from Clay - which you'll find everywhere in the forums :-) to convert the date ... and then do the modulo 10. That way you'll be sure about the year-leaps as well.

Regards,
Tom
A life ? Cool ! Where can I download one of those from ?
Jeff Schussele
Honored Contributor

Re: Cronjob every 10 days?

Hi Robert,

This is one that cron can definitely NOT do.
You should take a look at Clay Stephenson's calendar program.
Search the forum for "calendar program".

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
James R. Ferguson
Acclaimed Contributor

Re: Cronjob every 10 days?

Hi Robert:

Another useful method is to use 'at' (see the man pages). When your task runs, the last thing it does is re-schedule itself:

# at -f $HOME/mything now +10 days

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Cronjob every 10 days?

Hi Bob:

You are very close; you just need a better counter of days. You are the lucky winner - I recently finished my upgrade to caljd.sh - caljd.pl. It runs much faster but the commands/options are exactly the same. Your script should do something like this:

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

OFFSET=0
if [ $(( $(caljd.pl) + ${OFFSET}) % 10)) -eq 0 ]
then
echo "Execute your stuff"
else
echo "Do Nothing"
fi

You will need to play with the value of OFFSET (0-9) depending on which day you wish to start the cycle. You should install caljd.pl in /usr/local/bin. Caljd.pl -u gives full usage. You can also search the Forums for caljd.sh, if you like and use it instead. Vrsn 2.05 is the latest.



If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: Cronjob every 10 days?

Bob,

I like your logic. It should work except that it will not run on 4th of Jan which is 10days away from 360th day of the previous year. It will start from 10th day next year onwards.

My script would invoke an atjob to run it again after 10days using + 10 days option. Look at atjob man page.

There is also one nice utility caljd.sh posted by Clay that can be used to achive this. Search this site using caljd.sh string.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
harry d brown jr
Honored Contributor

Re: Cronjob every 10 days?

Bob,

Merely by using the julian date, it doesn't matter if it is a leap year or not. The problem will always be at the end of the year.

Non-leap year:
360 = transmit
365 = end of year

Next year
010 = next date of transmit
360 = last transmit for year

next year (leap)
010 = next date of transmit
360 = last transmit for year

next year (non-leap)
010 = next date of transmit.
...

So, no matter if it is a leap year or not, you WILL have issues at the end of each year.

It will either be 15 days for a non-leap year or 16 days for a leap year.

You need a better way, unless it doesn't matter at the end of the year. May I suggest that you always send the next files at the end of the year no matter the date difference?

live free or die
harry
Live Free or Die
A. Clay Stephenson
Acclaimed Contributor

Re: Cronjob every 10 days?

Hi again Bob:

To keep the Perl purists (nutballs?) off my case, you could also do this:

perl -e 'use integer; exit(((time() / 86400) + 0) % 10);'
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "Do your stuff"
else
echo "Do nothing"
fi

THe + 0 should be in the range 0-9 and serves exactly the same purpose as ${OFFSET} in my prior example.

Regards, Clay
If it ain't broke, I can fix that.
Pete Randall
Outstanding Contributor

Re: Cronjob every 10 days?

Robert,

Go with JRF's at suggestion but at the end of $HOME/mything do this:
echo "$HOME/mything" | at now +10 days

This will put the same job right back into the at queue for 10 days hence.

Good luck,
Pete

Pete
Paula J Frazer-Campbell
Honored Contributor

Re: Cronjob every 10 days?

Hi
Another way:-

Two cron jobs

First to run
echo "1" >> daycount.

Second

looks at day count and adds up the ones.
If equal to 10 then do then job if not dont.
If 10 then echo "1" > daycount (reset).

This will give no End of month/ year / leapyear problems.


HTH

Paula
If you can spell SysAdmin then you is one - anon
John Palmer
Honored Contributor

Re: Cronjob every 10 days?

You're right that there will be an issue with your method at new year.

If you are using modulo 0 for this year then I reckon that you'd need modulo 5 next year otherwise you'd have a 15 day interval from about 26th December to 10th January.

Here is a very simple C program that calculates the absolute day number since the UNIX epoch:-
#include
#include

main()
{
printf("%d\n",time(NULL)/86400);
}
------------------------------
You can extend that simply to provide you with that number of days modulo 10:
#include
#include

main()
{
time_t time_now;
int days;
int mod;

time_now=time(NULL);
days=time_now/86400;
mod=days%10;

printf("%d\n",mod);
}
------------------------------

Today is epoch modulo 9 whereas the Julian date is modulo 2 so you'd have to change your script to run the compiled program instead of date, then test for epoch modulo 7 instead of 0.

Regards,
John


Trever Furnish
Regular Advisor

Re: Cronjob every 10 days?

Or, if you'd rather recompile the wheel than reinvent it, this is EXACTLY what Anacron was designed for.

http://sourceforge.net/projects/anacron/

HTH,
Trever
Hockey PUX?
Pete Randall
Outstanding Contributor

Re: Cronjob every 10 days?

Sheesh!

Re-inventing, re-compiling, talk about overkill!! Just use at

at -f scriptname now +10 days

The last line in scriptname should be

echo "scriptname" |at now +10 days

Does exactly what you want. No fuss, no muss.

Pete

Pete
Robert Fisher_1
Frequent Advisor

Re: Cronjob every 10 days?

Wow! You guys are great. I left for lunch and my answers were waiting for me. I actually used Clay's caljd.pl script to do the trick.

Clay, I downloaded your Perl version on my PC before transferring it to my HP-UX server. Just for fun, I ran it on my PC and it worked there too!!! I also liked the usage message in the Perl version. It is a little clearer than the caljd.sh version.

I feel stupid because I had already used your caljd.sh script on another project and I should have been able to think of this solution myself.

Thanks everybody for the hints.

Bob