5
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?

Will
  • 24,082
  • 14
  • 97
  • 108
Amit Dugar
  • 274
  • 1
  • 2
  • 10
  • 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? – Ed Heal Jun 04 '16 at 08:47
  • from 2-31 days, _if it's a Sunday_ x.h should run, _non Sunday_ 2-31 days y.h should run – Amit Dugar Jun 04 '16 at 13:14

2 Answers2

13

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.

Amit Dugar
  • 274
  • 1
  • 2
  • 10
Will
  • 24,082
  • 14
  • 97
  • 108
  • This didn't work for me, I tried `20 13 2-31 * 7 /home/ubuntu/x.h` but it ran today too which is _Saturday_ – Amit Dugar Jun 04 '16 at 13:22
  • Sorry, fixed! Ubuntu's format was different. 0 is Sunday. Not sure why 7 ran on Saturday though. – Will Jun 04 '16 at 13:28
  • 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? – Amit Dugar Jun 04 '16 at 14:00
  • 1
    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*. – Will Jun 04 '16 at 14:07
  • thanks for the help, the approach worked but the test didn't :(. Although this worked `test $(date +\%u) -eq 7`Can you please make changes to the answer so that I can accept it? – Amit Dugar Jun 04 '16 at 14:57
  • No problem, glad to help! It's corrected now :) Thanks @AmitDugar. – Will Jun 04 '16 at 22:14
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()