System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

run a script every two days ?

Chris shi
Frequent Advisor

run a script every two days ?

hello everybody,
I want to run a script every two days,who can give me a good method?
Thank you very much!!!
16 REPLIES
V. Nyga
Honored Contributor

Re: run a script every two days ?

Hi,

you can define a cron job for every day.
In your script you can check a text file, start with '0', if it's '0' start the script and change the value to '1'.
If it's '1' change to '0' and exit your script without running.

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
Vibhor Kumar Agarwal
Esteemed Contributor

Re: run a script every two days ?

In the day field use "*/2"

This will run your cron every alternate day
Vibhor Kumar Agarwal
Michal Kapalka (mikap)
Honored Contributor

Re: run a script every two days ?

Hi,

create cron job,

this link describe which values is possible to use to become your schedule.

http://www.adminschoice.com/docs/crontab.htm

mikap
Sunny123_1
Esteemed Contributor

Re: run a script every two days ?

Hi

you can set in cronjob

In cronjob set weekdays as 1,3,5,7

means if you want to run script name job in every 2 days at 1:00 am then following will be the line you have to edit in cronjobs using crontab -e

00 1 * * 1,3,5,7 /home/job

Regards
Sunny
Suraj K Sankari
Honored Contributor

Re: run a script every two days ?

Hi,
As everyone said the simple and best solution is set your job into crontab!!

Total 6 field's are there in crontab file
min hour day month dayofweek command
* * * * * *
Example
# Trim log files 1st. and 16th. Of every month at 11 PM.
0 23 1,16 * * /trimlog

for more info about crontab
see man pages

Suraj
Steven Schweda
Honored Contributor

Re: run a script every two days ?

> In the day field use "*/2"

Is that in the "monthday" field or the
"weekday" field? I don't see this notation
in the HP-UX "man crontab" page:

http://docs.hp.com/en/B2355-60130/crontab.1.html

Please help me find it.

> In cronjob set weekdays as 1,3,5,7

You mean "crontab"? In the field described
as:

weekday
The day of the week, 0-6, 0=Sunday

where "7" seems to be conspicuously absent?

How many people believe that there is an
even number of days in a week? (Or a month,
every month? Or a year, every year?)

> [...] who can give me a good method?

Not so many people as you might have thought,
apparently.

> In your script you can check [...]

Probably not as reliable as a cron-only
scheme, but at least it can count. You could
store the day-of-year in the file, too, and
have the script check for any missed days, if
that sort of problem detection would be
useful.
Sunny123_1
Esteemed Contributor

Re: run a script every two days ?

Hi

You are right steven i think following cron job will help

0 1 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 * * /home/job

Regards
Sunny
James R. Ferguson
Acclaimed Contributor

Re: run a script every two days ?

Hi:

I think Volkmar has the most appropriate solution --- use a file into which you write either a "0" or a "1" every time your script executes. If on startup the file contains "0"; overwrite it with "1" and exit. If on startup the file contains "1" overwrite itwith "0" and continue your execution cycle.

There is no "/' notation in HP-UX's implementation of cron. This is a GNU/Linux offering.

Specifying a series of days in your crontab entry (e.g. 1,3,5 ... 29,31) doesn't work well since months contain a variable number of days (let alone compensating for leap years).

Regards!

...JRF...
V. Nyga
Honored Contributor

Re: run a script every two days ?

>> [...] who can give me a good method?

>Not so many people as you might have thought, apparently.

lol - thanks Steven

@Sunny - this will also not work, after the 31st the 1st follows, so no day between.

V.
*** Say 'Thanks' with Kudos ***
V. Nyga
Honored Contributor

Re: run a script every two days ?

Hi again,

if you don't like to read in a file, you also can check if a file exists.
So for example start with file 'file0.txt', if this file exists move it to 'file1.txt' and run your script. If it doesn't exist mv the file 'file1.txt' to 'file0.txt' and do nothing more.

(you also could write the current date to this file for 'problem detection' ...)

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
Steven Schweda
Honored Contributor

Re: run a script every two days ?

> [...] i think following cron job will help

Well, at least it doesn't fail _every_ week.

> [...] who can give me a good method?

I suppose that only a few failures every year
might be "good", for some values of "good".

Sigh.
Dennis Handly
Acclaimed Contributor

Re: run a script every two days ?

As mentioned you schedule it for every day and you check inside your script.

Depending on how accurately you care about "every" two days, you can simply check on the julian day of the year.
if (( $(date +%j) % 2 == 0 )); then
exit
fi

If you need to be accurate for the next decade or so, you can get the previous year and test for a multiple of 4.
Chris shi
Frequent Advisor

Re: run a script every two days ?

Thank you for everybody's reply!
I think V. Nyga's method is a better way and Steven Schweda is a responsible man~
I will add the script then.
Dennis Handly
Acclaimed Contributor

Re: run a script every two days ?

>Volkmar: you also can check if a file exists.

Slight variation. If the file exists, remove it and exit.
If it doesn't, touch it and continue.
Steven Schweda
Honored Contributor

Re: run a script every two days ?

> Slight variation. If the file exists,
> remove it and exit.
> If it doesn't, touch it and continue.

As usual, many things are possible.

Of course, if a come-and-go file is
accidentally deleted, then you can't detect
its absence as a problem. Other schemes are
harder to fool. Everything's complicated.
(Or else it should be.)
Dennis Handly
Acclaimed Contributor

Re: run a script every two days ?

>Steven: if a come-and-go file is accidentally deleted, then you can't detect its absence as a problem.

This is failsafe. It then does it the next day. So it is a feature. :-)

I'm assuming that "every two" is fuzzy and we aren't printing duplicate checks. ;-)