Run every 2nd and 4th Saturday of the month

前端 未结 5 1056
执念已碎
执念已碎 2020-12-16 00:21

What\'s the cron (linux) syntax to have a job run on the 2nd and 4th Saturday of the month?

5条回答
  •  执笔经年
    2020-12-16 00:50

    If you have it availble, you can use ncal to to get a calendar with day names along the side in the first column where you can grep for the day of the week in order to get the dates of the month of which that day falls on:

        August 2012
    Su     5 12 19 26
    Mo     6 13 20 27
    Tu     7 14 21 28
    We  1  8 15 22 29
    Th  2  9 16 23 30
    Fr  3 10 17 24 31
    Sa  4 11 18 25
    

    If you grep for "Sa" for Saturday, you'll get a line with the dates of the month that are Saturdays. If you use awk to parse out a particular column (awk '{print($3)}' for the "second" Saturday and awk '{print($5)}' for the fourth Saturday) and compare that to today's date (date +%e), then you'll know if the script needs to run or not.

    You can then build a script around this and chain it together in a crontab entry that runs on Saturdays. So when it runs on a Saturday, if the date equals the 2nd or 4th Saturday, exit cleanly and then use && in the crontab entry after this script to run the actual job. But if it it is the 1st or 3rd Saturday, exit 1 and then && won't run the job. (You'll get a cron error email saying it did not complete or whatever)

提交回复
热议问题