cronjobから特定の日時を除外

cronjobから特定の日時を除外

私は、ジョブが毎月第二、第四日曜日の午前1時から午前3時までを除いて、毎日、毎月、毎分、毎時間実行する必要があるという要件でcronジョブをスケジュールしようとしています。

私たちができる方法はありますか?私は成功せずに以下の方法を試してみました。

* 0-1,3-23 1-3,5-17,19-31 * 1-6

ベストアンサー1

cronこれらの時間ごとの例外を単独で構成することは不可能です。代わりに、これらの例外が発生したときにスクリプトが実行されないように、スクリプトにチェックを入れる必要があります。たとえば、次のようになります。

#!/bin/sh

# If it's sunday ...
if [ "$(date +%u)" = "7" ]; then
  # and it's the 2nd or ...
  if ( [ "$(date +%e)" -gt "7" ] && [ "$(date +%e)" -lt "15" ] ) || \
  # or the 4th sunday of the month ...
  ( [ "$(date +%e)" -gt "21" ] || [ [ "$(date +%e)" -lt "29" ] ); then
    # look if it's between 1 and 3 AM ...
    if [ "$(date +%k)" -ge "1" ] && [ "$(date +%k)" -le "3" ]; then
      # exit the script if all of the above have met
      exit
    fi
  fi
fi

# normal script continues here

スクリプトの先頭に追加します。

おすすめ記事