LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   configure crontab for every 2 days? (https://www.linuxquestions.org/questions/linux-newbie-8/configure-crontab-for-every-2-days-4175620792/)

businesscat 01-03-2018 10:04 AM

configure crontab for every 2 days?
 
Hello,

I need to configure a crontab every 2 days, but only obtain every 2nd day of the week/month

Has anyone an idea?

Thanks and happy new year to everyone

pan64 01-03-2018 10:39 AM

I would run in every day and create a filter to skip every second day or whatever you want.
You can use the command date (see format flags) to get day of week, day of month or anything you need.

TB0ne 01-03-2018 11:10 AM

Quote:

Originally Posted by businesscat (Post 5801091)
Hello,
I need to configure a crontab every 2 days, but only obtain every 2nd day of the week/month Has anyone an idea?

Your question is ambiguous.

Your subject line is "configure crontab for every 2 days"...your question then asks to run it every 2nd day of the week/month. That describes THREE different scenarios, either:
  1. Every other day
  2. The second day of each week
  3. The second day of each month
So which is it?

eager 01-03-2018 03:17 PM

Periodic crontab entry
 
Some versions of crontab allow you to put something like */2 or */3 to have the action taken ever two or three (in these examples) of whatever unit you are selecting. If you wanted to run "hello.sh" every 5 minutes, you could have a line in crontab like

*/12 * * * * hello.sh

or alternately

0,5,10,15,20,25,30,35,40,45,50,55 * * * * hello.sh

jtison 01-03-2018 04:10 PM

Periodic cron job
 
To amplify on what eager said, the third field is "day of month". You could put */2 in that field, knowing that on a transition from a 31-day (or 29-day) month to the next, you won't get an execution until the third day (the 2nd of the next month) after the 30th.

Unfortunately, you can't use any value higher than 23 in the "hour of day" (the second) field, so that won't help you.

If it were me, I'd live with the three-day gap at the end of certain months just to make it easier on myself.

If I were interested in getting a tiny bit more complex and executing every second day, no matter which month transition is pending, I'd write a script that takes the Julian day of year, modulos it by two, and executes the job when the result is one. Execute that script at a fixed time of day every day in your crontab. The only irregularity you'll have to deal with is at end of year, when your job will run on subsequent days (except leap years).

HTH.

smallpond 01-03-2018 04:32 PM

What jtison said. But if you really want every 2 days even across year boundaries then this will print 0 on even days and 1 on odd days. Add an offset of half a day (43200) to time() if you run it close to midnight and are worried about leap seconds.
Code:

perl -e '$day = int(time()/86400); print $day % 2,"\n"'
Another way to make odd time-dependent decisions is to touch a file when you run your command. Then check the file modification time using stat to see if it's time to run again.

businesscat 01-04-2018 04:43 AM

By the moment, I configure by the following:


Code:

50 7 */2 * * [execution file]
Every 48h to the 7,50AM hours


Quote:

Originally Posted by TB0ne (Post 5801135)
Your question is ambiguous.

Your subject line is "configure crontab for every 2 days"...your question then asks to run it every 2nd day of the week/month. That describes THREE different scenarios, either:
  1. Every other day
  2. The second day of each week
  3. The second day of each month
So which is it?

sorry for my ambiguity, I need execute a cron every 48h (2 days).

Fjor 01-06-2018 12:13 AM

Quote:

Originally Posted by eager (Post 5801302)
Some versions of crontab allow you to put something like */2 or */3 to have the action taken ever two or three (in these examples) of whatever unit you are selecting. If you wanted to run "hello.sh" every 5 minutes, you could have a line in crontab like

*/12 * * * * hello.sh

or alternately

0,5,10,15,20,25,30,35,40,45,50,55 * * * * hello.sh

According to the ISC cron manual (consulted using man 5 cron), the number x in */x indicates the number of steps skipped in the corresponding field. In the case depicted by eager, */12 in the minutes field will cause the action taken every 12 minutes, not five as stated, in this cron. Same way, analyzing the jtison's post, */2 in the day field is the same as 1-31/2, or "1,3,5...,29,31", so won't be two-day skipping at 31st of the month in this version of cron, i.e, go at 31st, then go at 1st of the next month. Conclusion: man is your friend, as always.

Quoting the man section referred to:
Quote:

The time and date fields are:

field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

A field may be an asterisk (*), which always stands for ``first-last''.
(...)
Step values can be used in conjunction with ranges. Following a range with ``/<number>'' speci‐
fies skips of the number's value through the range. For example, ``0-23/2'' can be used in the
hours field to specify command execution every other hour (the alternative in the V7 standard is
``0,2,4,6,8,10,12,14,16,18,20,22''). Steps are also permitted after an asterisk, so if you want
to say ``every two hours'', just use ``*/2''."


All times are GMT -5. The time now is 04:57 AM.