program tip

3 일마다 크론 작업

radiobox 2020. 10. 18. 09:14
반응형

3 일마다 크론 작업


3 일마다 cronjob 을 실행할 수 있습니까? 또는 한 달에 10 번 정도.


3 일마다 실행 ...

0 0 */3 * *

어떻게에 대한?

1 일, 4 일, 7 일 등과 같은 특정 날짜에 실행되도록하려면 스크립트에 해당 월의 현재 날짜를 확인하는 조건부 만 있으면됩니다.

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

또는 @mario가 지적했듯이 date ( 'k')를 사용하여 날짜를 기준으로하는 대신 날짜를 가져올 수 있습니다.


* * */3 * *  that says, every minute of every hour on every three days. 

0 0 */3 * *  says at 00:00 (midnight) every three days.

cron은 "stateless"이기 때문에 "frequencies"를 정확하게 표현할 수 없으며 현재 시간과 지속적으로 일치하는 "패턴"만 표현할 수 있습니다.

질문을 바꾸면 "2 박 이내에 실행 된 밤을 건너 뛰는 것을 제외하고 매일 오전 00:01에 cronjob을 실행할 수 있습니까?" 크론이 현재 시간을 작업 요청 시간 패턴과 비교할 때 크론이 과거에 작업을 실행했는지 알 수있는 방법은 없습니다.

일반적으로 반복되는 이벤트를 종료하고 완전히 새로운 이벤트를 다시 시작해야합니다. 이것은 복잡한 캘린더 앱이 반복 패턴을 나타내는 방식에 대한 제한된 표현을 보여줍니다. 물론 캘린더에는 많은 상태가 있습니다. 각 개별 이벤트는 [대부분의 캘린더 앱에서] 독립적으로 삭제하거나 일정을 조정할 수 있습니다.

또한 성공하면 3 일마다 작업을 수행하고 싶지만 마지막 작업이 실패한 경우 즉시 다시 시도하거나 다음날 밤 (3 일 더 기다리지 않음) 또는 더 빨리 1 시간 후 (다시 시도 중지) 아침 도착시). 분명히, 크론은 당신의 작업이 성공했는지 알 수 없었고 패턴은 더 빈번한 "재시도"일정을 표현할 수도 없습니다.

어쨌든-원하는 것을 스스로 할 수 있습니다. 스크립트를 작성하고 cron에게 밤마다 오전 00:01에 실행하도록 지시하십시오. 이 스크립트는 "마지막 실행"을 기록하는 항목 *의 타임 스탬프를 확인할 수 있으며, 3 일 이상 전 **이면 작업을 수행하고 "마지막 실행"타임 스탬프를 재설정합니다.

(* 타임 스탬프 표시기는 조작하고 검사 할 수 있지만 크론은 할 수없는 약간의 지속 된 상태입니다.)

** 사람이 읽을 수있는 시계 시간을 사용하는 경우 시간 산술에주의하십시오. 1 년에 두 번, 어떤 날에는 하루에 23 시간 또는 25 시간이 있으며, 02 : 00-02 : 59는 ​​하루에 두 번 발생하거나 모두. 이를 방지하려면 UTC를 사용하십시오.


저는 크론 전문가는 아니지만 다음은 어떻습니까?

0 */72 * * *

72 시간마다 중단없이 실행됩니다.

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


0 0 1-30/3 * *

이것은 1 일부터 3 일마다 실행됩니다. 다음은 20 개의 예정된 실행입니다.

  • 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

어때 :

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 runs every day.

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


You should learn the basics of crontab.

Edit the cron by command crontab -e and then ⌃ (CTRL) + X then Y and finally press ENTER (return) on mac to save the file. You can check the new crons have been installed of not by crontab -l

A crontab file has five fields for specifying mins, hours, the day of the month, month, and the day of the week followed by the command to be 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 value field above means all legal values as in braces for that column.

Here, I wrote a detailed post about it: Setup Cron in Unix


I don't think you have what you need with:

0 0 */3 * * ## <<< WARNING!!! CAUSES UNEVEN INTERVALS AT END OF MONTH!!

Unfortunately, the */3 is setting the interval on every n day of the month and not every n days. See: explanation here. At the end of the month there is recurring issue guaranteed.

1st  at 2019-02-01 00:00:00
then at 2019-02-04 00:00:00 << 3 days, etc. OK
then at 2019-02-07 00:00:00
...
then at 2019-02-25 00:00:00
then at 2019-01-28 00:00:00
then at 2019-03-01 00:00:00 << 1 day WRONG
then at 2019-03-04 00:00:00
...

According to this article, you need to add some modulo math to the command being executed to get a TRUE "every N days". For example:

0 0 * * *  bash -c '(( $(date +\%s) / 86400 \% 3 == 0 )) && runmyjob.sh

In this example, the job will be checked daily at 12:00 AM, but will only execute when the number of days since 01-01-1970 modulo 3 is 0.

If you want it to be every 3 days from a specific date, use the following format:

0 0 * * *  bash -c '(( $(date +\%s -d "2019-01-01") / 86400 \% 3 == 0 )) && runmyjob.sh

It would be simpler if you configured it to just run e.g. on monday and thursdays, which would give it a 3 and 4 day break.

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

if (! (date("z") % 3)) {
     exit;
}

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

I thought all you needed 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. so 7/3 would mean every three days starting on the 7th of the month, etc.


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

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

참고URL : https://stackoverflow.com/questions/4549542/cron-job-every-three-days

반응형