10

I have a cron job that runs at 0 0 * * 0/2 -- i.e. every other Sunday. It just ran now, for instance. However, I want it to run on the other Sundays -- next Sunday, rather than this one. Another way of putting it: I presume it's running on even-numbered Sundays of the year, but I want it to run on odd-numbered Sundays.

I don't see anything in "man 5 crontab" that would help me with this so I presume I need to drop back to running the cronjob at 0 0 * * 0 and then do something in the command I run to check which Sunday it is. Can anyone help me with some suitable command line trickery for this?

ETA: have realised that my 0/2 doesn't do what I originally thought (every second Sunday) at all. It runs every Sunday, as long as that day of the week is divisible by two, which of course it is. D'oh. Anyway, the fundamental requirement remains: run a command on odd-numbered Sundays.

  • "odd-numbered" as in "The First, Third, and Fifth Sunday"? – voretaq7 Apr 29 '13 at 21:11
  • 1
    No, as in 1st, 3rd, 5th, 7th,...49th, 51st week of the year. In other words, fortnightly, on the Sundays that fall in odd-numbered weeks of the year. – Skud Apr 30 '13 at 0:23
  • Sorry - cron won't do that for you. You'll need to have your script check whether it's the right week for you. (Also, if you remember me from a Certain Place - HI!!! waves) – Jenny D May 20 '13 at 10:06
3

I won't pretend to have attempted this particular task myself, but this Stack Overflow question has some answers that show potential command line trickery: https://stackoverflow.com/questions/350047/how-to-instruct-cron-to-execute-a-job-every-second-week

  • None of those make cron run on the odd numbered weeks. They all simply do every other. Some in extremely exotic and "creative" ways that I wouldn't recommend either. – Chris S Apr 28 '13 at 2:17
  • The top-voted answer looks to run every week and then rely on command line trickery to determine whether it's the appropriate week. – nmjk Apr 28 '13 at 2:19
  • 1
    Yes, but it's modulo 2, which means it's going to run on the even numbered weeks. It might be possible to stack multiple eval blocks to add one and then do the modulo 2. That's ugly at best, but I suppose it might work. – Chris S Apr 28 '13 at 2:21
  • 1
    From the linked answer, this looks like it has potential: date +\%W \% 2 > /dev/null || whatever... However, to get the odd numbered weeks, I'd want to do "&& whatever" instead of || – Skud Apr 28 '13 at 2:30
  • 1
    So, asking for clarification. Are you wanting to determine "every odd week" counting by week-of-year, or counting by week-of-month? I'm pretty sure the latter is possible but I'd have to find some old snippets I've used in the past. – nmjk Apr 28 '13 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.