Cron Job to run on a range of days only on a particular day of week
Asked Answered
F

2

7
0 0 2-31 * sun         /home/ubuntu/x.h
0 0 2-31 * mon-sat     /home/ubuntu/y.h

This ends up running both of them. Am I doing something wrong here?

Frasch answered 4/6, 2016 at 6:44 Comment(2)
Of course it will run both of them eventually. I guess that you mean on the same day. If so do you have evidence for this?Prosthetics
from 2-31 days, if it's a Sunday x.h should run, non Sunday 2-31 days y.h should runFrasch
M
15

This is the crontab format:

* * * * *
| | | | |
| | | | +---- Day of the Week   (range: 0-6, 0 standing for Sunday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month  (range: 1-31)
| +---------- Hour              (range: 0-23)
+------------ Minute            (range: 0-59)

Ubuntu man 5 crontab says:

  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)

So, this should work for you:

0 0 2-31 * 0         /home/ubuntu/x.h
0 0 2-31 * 1-6       /home/ubuntu/y.h

I'm not sure why 7 would run on Saturday--is your system time accurate and in the right timezone?

Edit: Ah, yes, unfortunately you cannot specify both the day of the week and the day of the month. From man 5 crontab:

Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

So, the answer is:

0 0 2-31 * *       test $(date +\%u) -eq 7 && /home/ubuntu/x.h
0 0 2-31 * *       test $(date +\%u) -ne 7 && /home/ubuntu/y.h

$(date '+%u') returns 1-7 representing Monday thru Sunday. Try echo $(date '+%u') for an example.

Monarchist answered 4/6, 2016 at 9:30 Comment(6)
This didn't work for me, I tried 20 13 2-31 * 7 /home/ubuntu/x.h but it ran today too which is SaturdayFrasch
Sorry, fixed! Ubuntu's format was different. 0 is Sunday. Not sure why 7 ran on Saturday though.Monarchist
It still didn't work!, this is weird, does date of month and day of week stack? I tried running 13 58 * * 6 and it worked fine but adding a date range overrode the date of week check. Am I right?Frasch
Ah, wow, I learned something! See my edit. I had no idea that crontab behaved this way :) If both date specifiers are present, either will have to match, not both.Monarchist
thanks for the help, the approach worked but the test didn't :(. Although this worked test $(date +\%u) -eq 7Can you please make changes to the answer so that I can accept it?Frasch
No problem, glad to help! It's corrected now :) Thanks @AmitDugar.Monarchist
I
0
from datetime import datetime
from datetime import timedelta
import urllib.request

//urls to hit
urls=["https//:example1.com","https//:example2.com"
]

//function to hit url
def call(url):
 urllib.request.urlopen(url)

//function to get date 
def get_month_diff(current,nom):
    m1= current
    m2=m1 - timedelta(days=nom*30)
    m3=current
    m4=m2.replace(day=1)
    m5=m3.replace(day=1)-timedelta(days=1)
    list=str(m4).split(" ")[0].split("-")
    list.reverse()
    startDate="-".join(list)
    list1=str(m5).split(" ")[0].split("-")
    list1.reverse()
    endDate="-".join(list1)
    for i in range (0,5):
       call(urls[i]+""+startDate+"/"+endDate)
//main execution function
def solve():
    month=str(datetime.today()).split("-")[1]
    if month in ["01","04","07","10"] :get_month_diff(datetime.today(),3)
    if month in ["01","07"]:get_month_diff(datetime.today(),6)
    get_month_diff(datetime.today(),1)
solve()
Isidroisinglass answered 27/4, 2021 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.