Cron every three days

Is it possible to run a cronjob every three days? Or maybe 10 times / month.

+45
cron
Dec 28 '10 at 21:08
source share
10 answers

Run it every three days ...

0 0 */3 * * 

How about this?

If you want it to run on certain days of the month, for example, 1st, 4th, 7th, etc., then you can just have a conditional expression in a script that checks the current day of the month.

 if (((date('j') - 1) % 3)) exit(); 

or, as @mario points out, you can use the date ('k') to get the day of the year instead of doing it depending on the day of the month.

+65
Dec 28 '10 at 21:12
source share
 * * */3 * * that says, every minute of every hour on every three days. 0 0 */3 * * says at 00:00 (midnight) every three days. 
+44
Jan 16 2018-11-22T00:
source share

Since cron is “stateless”, it cannot express “frequencies” exactly, but only “patterns” that it (apparently) constantly matches the current time.

To paraphrase my question, it becomes more obvious: "Is it possible to run a cronjob at 00:01 in the morning every night, except for the passes when it ran for 2 nights?" When cron compares the current time with the job request time patterns, cron cannot know if it has done its job in the past.

(You may be able to write cron with a state that records past jobs and thus includes templates for matching this state, but this is not the standard cron included in most operating systems. Such a system will be complicated, requiring the introduction of the concept of when such patterns are “reset.” For example, is the reset pattern when the time changes (ie, the crontab entry is being revised)? Look at your favorite calendar application to see how difficult it can be expressed Repeating patterns of planned events and images Please note that they do not have a problem with reset, because the start calendar event has a natural date “start” a / k / a “reset.” Try rescheduling a recurring calendar event every week for a week delay, for example, Christmas. you need to stop this recurring event and restart a brand new one, which illustrates the limited expressiveness of how even complex calendar applications present repeating patterns. And, of course, hav calendars have many states - each individual event can be deleted or rescheduled independently [in most calendar applications]).

In addition, you will probably want to do your work every third night if successful, but if the latter failed, try again immediately, perhaps the next night (do not wait another 3 days) or even earlier than an hour later (but stop try again on morning arrival). It is clear that cron could not know if your work was successful, and the template also could not express an alternative more frequent “repeat” chart.

ANYWAY-- You can do what you want. Write a script, tell cron to run it at night at 00:01. This script can check the timestamp of something * that records the "last run", and if it was> 3 days ago **, run the task and reset the time stamp of the "last run".

(* this timestamped indicator is a slightly persistent state that you can manipulate and check, but which cron cannot)

** Be careful with time arithmetic if you use a human-readable clock time - twice a year, on some days - 23 or 25 hours a day, and 02: 00-02: 59 occurs twice in one day or not at all. Use UTC to avoid this.

+9
Oct 17 '14 at 20:58
source share
 0 0 1-30/3 * * 

This will work every three days, starting on the 1st. Here are 20 scheduled runs -

  • 2015-06-01 00:00:00
  • 2015-06-04 00:00:00
  • 2015-06-07 00:00:00
  • 2015-06-10 00:00:00
  • 2015-06-13 00:00:00
  • 2015-06-16 00:00:00
  • 2015-06-19 00:00:00
  • 2015-06-22 00:00:00
  • 2015-06-25 00:00:00
  • 2015-06-28 00:00:00
  • 2015-07-01 00:00:00
  • 2015-07-04 00:00:00
  • 2015-07-07 00:00:00
  • 2015-07-10 00:00:00
  • 2015-07-13 00:00:00
  • 2015-07-16 00:00:00
  • 2015-07-19 00:00:00
  • 2015-07-22 00:00:00
  • 2015-07-25 00:00:00
  • 2015-07-28 00:00:00
+4
May 4 '15 at 7:08
source share

What about:

 00 00 * * * every 3 days && echo test 

Where every is a script:

 #!/bin/sh case $2 in days) expr `date +%j` % $1 = 0 > /dev/null ;; weeks) expr `date +%V` % $1 = 0 > /dev/null ;; months) expr `date +%m` % $1 = 0 > /dev/null ;; esac 

So it works every day.

Using */3 is performed on the 3rd, 6th, ... 27th, 30th months, but then the 31st day is incorrect after a month. every script is incorrect only after the end of the year.

+2
Jul 29 '15 at 11:13
source share

It would be easier if you configured it to just run, for example. on Monday and Thursday, which would give him a 3 and 4-day break.

Otherwise, configure it to run daily, but first make your php cron script with:

 if (! (date("z") % 3)) { exit; } 
+1
Dec 28 '10 at 21:13
source share

If you want it to run on certain days of the month, for example, 1st, 4th, 7th, etc., then you can just have a conditional expression in a script that checks the current day of the month.

I thought that all you need for this was instead of * / 3, which means every three days, use 1/3, which means every three days, starting on the 1st of the month. therefore, 7/3 will mean every three days, starting on the 7th of the month, etc.

0
Apr 10 '15 at 13:34
source share

You should learn the basics of crontab .

Edit cron with crontab -e and then ⌃ (CTRL) + X , then Y and finally press ENTER (return) on mac to save the file. You can verify that the new crosses are not installed on crontab -l

There are five fields in the crontab file to indicate the minutes, hours, days, months, months, and days of the week, followed by a command to run at that interval.

 * * * * * command to be executed - - - - - | | | | | | | | | +----- day of week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59) 

* in the values ​​field above means all valid values, as in braces for this column.

Here I wrote a detailed post about this: Configuring Cron on Unix

0
Jun 08 '17 at 12:22
source share

I'm not a cron specialist, but what about:

 0 */72 * * * 

It will work every 72 hours without interruption.

https://crontab.guru/#0_/72___

0
Jul 05 '17 at 15:49
source share

0 0 * * * [ $(($(( date +% - j - 1)) % 3)) == 0 ] && script

Get the day of the year from date shifted by 1 to start at 0, check if it is modulo three.

0
Nov 03 '17 at 17:53 on
source share



All Articles